Skip to content

Getting Started

Both packages target net8.0, net9.0, and net10.0.

Terminal window
dotnet add package Ambystech.Neo4j.Repository
dotnet add package Ambystech.Neo4j.Repository.Contracts

Provide the connection details through any IConfiguration source. appsettings.json, environment variables, and user secrets all work:

{
"Neo4j-Uri": "bolt://localhost:7687",
"Neo4j-User": "neo4j",
"Neo4j-Password": "password"
}

AddNeo4jRepository() reads the configuration keys above and registers IDriver as a singleton. Register one converter and one repository per entity type.

builder.Services.AddNeo4jRepository();
builder.Services.AddSingleton<INodeConverter<User>, UserConverter>();
builder.Services.AddScoped<IBaseGraphRepository<User>, UserRepository>();

Inherit from BaseNode and mark relationship properties with [GraphField]:

public class User : BaseNode
{
public string Name { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
[GraphField(relationshipTypes: new[] { RelationshipTypes.LIKE },
targetNodeLabel: nameof(Post),
direction: Direction.Outgoing)]
public List<Post> LikedPosts { get; set; } = new();
}
var users = serviceProvider.GetRequiredService<IBaseGraphRepository<User>>();
var created = await users.CreateAsync(new User { Name = "Ada", Email = "ada@example.com" });
var all = await users.GetAllAsync();
var one = await users.GetByIdAsync(created.Id);

See Packages → Repository for the full IBaseGraphRepository<T> surface.