Skip to content
Farhad Nowzari edited this page May 18, 2024 · 6 revisions

This library is created with dotnet 8, this means the host project must be implemented with dotnet 8 to be able to integrate this library.
To get started with the library, you can install it with the following command.

dotnet add package Neo4j.Berries.OGM

Configuration

First make sure you have added the following to your appsettings.json. The OGM library will use it to make a connection with neo4j.

{
    "Neo4j": {
        "Url": "",
        "Username": "",
        "Password": "",
        "Database": "" //optional
    }
}

Note: In case it is needed to use the default database, do not set the Database property. When this property is set, the library will open the sessions against the mentioned database.

GraphContext

As in Ef Core we need to extend the DbContext to define our DbSet<>, We need here also to extend the GraphContext to define our NodeSet<>. Each NodeSet<> is considered a repository since the CRUD to each node is isolated to each NodeSet<>. You can define your graph context like below.

public class MyGraphContext(Neo4jOptions options): GraphContext(options) 
{
    public NodeSet<Movie> Movies { get; private set; }
    public NodeSet<Person> People { get; private set; }
}

Now that you have the GraphContext set up, you can add it to the dotnet's dependency injection.

builder.Services.AddNeo4j<MyGraphContext>(builder.Configuration, typeof(Program).Assembly);

or

builder.Services.AddNeo4j<ApplicationGraphContext>(builder.Configuration, options =>
{
    options
        .ConfigureFromAssemblies(typeof(Program).Assembly);
});

Note

The GraphContext exposes DatabaseContext and you can access an open session to run custom cypher queries.

graphContext.Database.Session.Run(cypher);
Clone this wiki locally