Using Neo4j from .NET

Goals
If you are a .NET developer, this guide provides an overview of options for connecting to Neo4j. While this guide is not comprehensive it will introduce the different drivers and link to the relevant resources.
Prerequisites
You should be familiar with graph database concepts and the property graph model. You should have created an Neo4j AuraDB cloud instance, or installed Neo4j locally

Intermediate

Neo4j for .NET Developers

dotnet logo

Neo4j provides drivers which allow you to make a connection to the database and develop applications which create, read, update, and delete information from the graph.

Neo4jDotNetDriver

The Neo4j .NET driver is officially supported by Neo4j and connects to the database using the binary protocol. It aims to be minimal, while being idiomatic to .NET.

Please note that the following example makes use of the Neo4j.Driver.Simple package that implements a blocking interface around the 'main' driver. This is to aid in clarity and simplicity for those just starting out with the Neo4j .Net driver.

PM> Install-Package Neo4j.Driver.Simple-4.4.0

For real projects, the Neo4j.Driver package should be used instead.

PM> Install-Package Neo4j.Driver-4.4.0
public class HelloWorldExample : IDisposable
{
    private bool _disposed = false;
    private readonly IDriver _driver;

    ~HelloWorldExample() => Dispose(false);

    public HelloWorldExample(string uri, string user, string password)
    {
        _driver = GraphDatabase.Driver(uri, AuthTokens.Basic(user, password));
    }

    public void PrintGreeting(string message)
    {
        using (var session = _driver.Session())
        {
            var greeting = session.WriteTransaction(tx =>
            {
                var result = tx.Run("CREATE (a:Greeting) " +
                                    "SET a.message = $message " +
                                    "RETURN a.message + ', from node ' + id(a)",
                    new {message});
                return result.Single()[0].As<string>();
            });
            Console.WriteLine(greeting);
        }
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (_disposed)
            return;

        if (disposing)
        {
            _driver?.Dispose();
        }

        _disposed = true;
    }

    public static void Main()
    {
        using (var greeter = new HelloWorldExample("bolt://localhost:7687", "neo4j", "password"))
        {
            greeter.PrintGreeting("hello, world");
        }
    }
}

Driver Configuration

From Neo4j version 4.0 and onwards, the default encryption setting is off by default and Neo4j will no longer generate self-signed certificates. This applies to default installations, installations through Neo4j Desktop and Docker images. You can verify the encryption level of your server by checking the dbms.connector.bolt.enabled key in neo4j.conf.

Table 1. Table Scheme Usage
Certificate Type Neo4j Causal Cluster Neo4j Standalone Server Direct Connection to Cluster Member

Unencrypted

neo4j

neo4j

bolt

Encrypted with Full Certificate

neo4j+s

neo4j+s

bolt+s

Encrypted with Self-Signed Certificate

neo4j+ssc

neo4j+ssc

bolt+ssc

Neo4j AuraDB

neo4j+s

N/A

N/A

Please review your SSL Framework settings when going into production. If necessary, you can also generate certificates for Neo4j with Letsencrypt

Name

Version

Authors

Neo4jDotNetDriver

4.4.0

The Neo4j Team, Charlotte Skardon, Martin Jensen

NuGet Package

.NET Example, .NET core example

Neo4j Online Community

Docs

API

Source

The Example Project

The Neo4j example project is a small, one page webapp for the movies database built into the Neo4j tutorial. The front-end page is the same for all drivers: movie search, movie details, and a graph visualization of actors and movies. Each backend implementation shows you how to connect to Neo4j from each of the different languages and drivers.

You can learn more about our small, consistent example project across many different language drivers here. You will find the implementations for all drivers as individual GitHub repositories, which you can clone and deploy directly.

Neo4j Community Drivers

Members of the each programming language community have invested a lot of time and love to develop each one of the community drivers for Neo4j, so if you use any one of them, please provide feedback to the authors.

The community drivers have been graciously contributed by the Neo4j community. Many of them are fully featured and well-maintained, but some may not be. Neo4j does not take any responsibility for their usability.

Neo4jClient

A .NET client for Neo4j, which makes it easy to write Cypher queries in C# with IntelliSense. It also supports basic CRUD and legacy indexing.

Source

https://github.com/DotNet4Neo4j/neo4jclient

NuGet Package

https://nuget.org/packages/neo4jclient

Authors

Charlotte Skardon Tatham Oddie

Docs

https://github.com/DotNet4Neo4j/Neo4jClient/wiki

Example

https://github.com/neo4j-examples/movies-dotnet-neo4jclient

Protocol

Bolt, Http

Neo4j.Driver.Extensions

Neo4j.Driver.Extensions provides a set of extension methods to the official driver API, aiming at reducing boilerplate and easing mapping to entity classes.

Source

https://github.com/DotNet4Neo4j/Neo4j.Driver.Extensions

NuGet Package

https://nuget.org/packages/neo4j.driver.extensions

Authors

Charlotte Skardon

Docs

Introduction blogpost