C# quick start
At the end of this guide, you will have created a simple . NET console Hello, World!
program that connects to the Memgraph database and executes simple
queries.
Prerequisites
If you're new to Memgraph or you're in a developing stage, we recommend using the Memgraph Platform. Besides the database, it also includes all the tools you might need to analyze your data, such as command-line interface mgconsole, web interface Memgraph Lab and a complete set of algorithms within a MAGE library.
Ensure Docker (opens in a new tab) is running in the background. Depending on your operating system, execute the appropriate command in the console:
For Linux and macOS:
curl https://install.memgraph.com | sh
For Windows:
iwr https://windows.memgraph.com | iex
The command above will start Memgraph Platform, which includes Memgraph database, Memgraph Lab and Memgraph MAGE. Memgraph uses Bolt protocol to communicate with the client using the exposed 7687 port. Memgraph Lab is a web application you can use to visualize the data. It's accessible at http://localhost:3000 (opens in a new tab) if Memgraph Platform is running correctly. The 7444 port enables Memgraph Lab to access and preview the logs, which is why both of these ports need to be exposed.
For more information visit the getting started guide on how to run Memgraph with Docker.
For Memgraph < 2.11, in order for the Neo4j driver to work, you need modify configuration
setting
--bolt-server-name-for-init
. When running Memgraph, set
--bolt-server-name-for-init=Neo4j/5.2.0
. If you use other version of Neo4j
driver, make sure to put the appropriate version number.
Driver
Please note that the code samples in this guide utilize the
Neo4j.Driver.Simple
package which implements a blocking interface around the
'main' driver. It should be used as a tool for getting started quickly. The
Neo4j.Driver
package contains the official and complete driver for real-world
projects. The driver documentation can be found here: Neo4j . NET
Driver (opens in a new tab).
The Blueprint41 (opens in a new tab), .NET ORM, also supports Memgraph. Blueprint 41 supports the definition of the object model as a schema. It offers refactoring scripts written in CSharp, which you can add to your project. When you run your program with an older graph version, the upgrade script will automatically be executed against the graph. It also supports the generation of type-safe data objects, so you know at compile time if your code is compatible with the latest upgrades.
Basic Setup
We'll be using Visual Studio 2022 on Windows 10 to connect a simple . NET
console application to a running Memgraph instance. If you're using a different
IDE, the steps might be slightly different, but the code is either the same or
very similar.
Let's jump in and connect a simple program to Memgraph.
1. Open Visual Studio and create a new project.
2. Find and
select the Console App (. NET Core) template by using the search box.
3. Name your project MemgraphApp, choose an appropriate location for
it, and click Create.
4. Select the Tools > Manage NuGet
Packages menu command.
5. Once the window opens, search for the
Neo4j.Driver.Simple.
6. Select the appropriate driver and click Add
package.
Now, you should have the newest version of the driver installed and can proceed to copy the following code into the Program.cs file.
using Neo4j.Driver;
namespace MemgraphApp
{
public class Program
{
public static void Main()
{
string message = "Hello, World!";
using var _driver = GraphDatabase.Driver("bolt://localhost:7687", AuthTokens.None);
using var session = _driver.Session();
var greeting = session.ExecuteWrite(tx =>
{
var result = tx.Run("CREATE (n:FirstNode) " +
"SET n.message = $message " +
"RETURN 'Node ' + id(n) + ': ' + n.message",
new { message });
return result.Single()[0].As<string>();
});
Console.WriteLine(greeting);
}
}
}
Once you run the program, you should see an output similar to the following:
Node 1: Hello, World!
To configure indexes and constraints properly, do one operation at a time and
use the non-transactional API: cs await session.RunAsync(query: "CREATE INDEX ON :FirstNode"); await session.RunAsync(query: "CREATE INDEX ON :FirstNode(message)");
Alternative Setup
If you want to try out more complex operations, feel free to use the refactored code below.
using Neo4j.Driver;
namespace MemgraphApp
{
public class Program : IDisposable
{
private readonly IDriver _driver;
public Program(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.ExecuteWrite(tx =>
{
var result = tx.Run("CREATE (n:FirstNode) " +
"SET n.message = $message " +
"RETURN 'Node ' + id(n) + ': ' + n.message",
new { message });
return result.Single()[0].As<string>();
});
Console.WriteLine(greeting);
}
}
public void Dispose()
{
_driver?.Dispose();
}
public static void Main()
{
using (var greeter = new Program("bolt://localhost:7687", "", ""))
{
greeter.PrintGreeting("Hello, World!");
}
}
}
}
If you encounter serialization errors while using C# client, we recommend referring to our Serialization errors page for detailed guidance on troubleshooting and best practices.