Home

Awesome

Neo4j .NET Driver

This repository contains the official Neo4j driver for .NET.

API Docs | Driver Manual | Example Web App | Change Log

This document covers the usage of the driver; for contribution guidance, see Contributing.

Installation

Neo4j publishes its .NET libraries to NuGet with the following targets:

To add the latest NuGet package:

> dotnet add package Neo4j.Driver

Versions

Starting with 5.0, the Neo4j drivers moved to a monthly release cadence. A new minor version is released on the last Thursday of each month to maintain versioning consistency with the core product (Neo4j DBMS), which also has moved to a monthly cadence.

As a policy, Neo4j will not release patch versions except on rare occasions. Bug fixes and updates will go into the latest minor version; users should upgrade to a later version to patch bug fixes. Driver upgrades within a major version will never contain breaking API changes, excluding the Neo4j.Driver.Preview namespace reserved for the preview of features.

See also: https://neo4j.com/developer/kb/neo4j-supported-versions/

Synchronous and Reactive driver extensions

Strong-named

A strong-named version of each driver package is available on NuGet Neo4j.Driver.Signed. The strong-named packages contain the same version of their respective packages with strong-name compliance. Consider using the strong-named version only if your project is strong-named or requires strong-named dependencies.

To add the strong-named version of the driver to your project using the NuGet Package Manager:

> Install-Package Neo4j.Driver.Signed

Getting started

Connecting to a Neo4j database:

using Neo4j.Driver;

await using var driver = GraphDatabase.Driver("bolt://localhost:7687", AuthTokens.Basic("neo4j", "password"));

There are a few points to highlight when adding the driver to your project:

Verifying connectivity:

await driver.VerifyConnectivityAsync();

To ensure the credentials and URLs specified when creating the driver, you can call VerifyConnectivityAsync on the driver instance. If either configuration is wrong, the Task will result in an exception.

Executing a single query transaction:

await driver.ExecutableQuery("CREATE (:Node{id: 0})")
    .WithConfig(new QueryConfig(database:"neo4j"))
    .ExecuteAsync();

As of version 5.10, The .NET driver includes a fluent querying API on the driver's IDriver interface. The fluent API is the most concise API for executing single query transactions. It avoids the boilerplate that comes with handling complex problems, such as results that exceed memory or multi-query transactions.

Remember to specify a database.

    .WithConfig(new QueryConfig(database:"neo4j"))

Always specify the database when you know which database the transaction should execute against. By setting the database parameter, the driver avoids a roundtrip and concurrency machinery associated with negotiating a home database.

Getting Results

var response = await driver.ExecutableQuery("MATCH (n:Node) RETURN n.id as id")
    .WithConfig(dbConfig)
    .ExecuteAsync();

The response from the fluent APIs is an EagerResult<IReadOnlyList<IRecord>> unless we use other APIs; more on that later. EagerResult comprises of the following:

Decomposing EagerResult

var (result, _, _) = await driver.ExecutableQuery(query)
    .WithConfig(dbConfig)
    .ExecuteAsync();
foreach (var record in result)
    Console.WriteLine($"node: {record["id"]}")

EagerResult allows you to discard unneeded values with decomposition for an expressive API.

Mapping

var (result, _, _) = await driver.ExecutableQuery(query)
    .WithConfig(dbConfig)
    .WithMap(record => new EntityDTO { id = record["id"].As<long>() })
    .ExecuteAsync();

Types

Values in a record are currently exposed as of object type. The underlying types of these values are determined by their Cypher types.

The mapping between driver types and Cypher types are listed in the table bellow:

Cypher TypeDriver Type
nullnull
ListIList< object >
MapIDictionary<string, object>
Booleanboolean
Integerlong
Floatfloat
Stringstring
ByteArraybyte[]
PointPoint
NodeINode
RelationshipIRelationship
PathIPath

To convert from object to the driver type, a helper method ValueExtensions#As<T> can be used:

IRecord record = await result.SingleAsync();
string name = record["name"].As<string>();

Temporal Types - Date and Time

The mapping among the Cypher temporal types, driver types, and convertible CLR temporal types - DateTime, TimeSpan and DateTimeOffset - (via IConvertible interface) are as follows:

Cypher TypeDriver TypeConvertible CLR Type
DateLocalDateDateTime, DateOnly(.NET6+)
TimeOffsetTimeTimeOnly(.NET6+)
LocalTimeLocalTimeTimeSpan, DateTime
DateTimeZonedDateTimeDateTimeOffset
LocalDateTimeLocalDateTimeDateTime
DurationDuration---