How to setup RavenDB with ASP.NET Core 8 application
Table of contents
What you will learn
- How to connect your ASP.NET Core 8 application to a RavenDB database
- How to confirm your connection is established
Introduction
Connecting your C# console application to RavenDB is a simple process. By the end of this tutorial, we will connect your C# app to the RavenDB database and be ready to integrate it into your projects. Whether you’re new to using RavenDB or simply looking for a straightforward setup, this tutorial will smoothly guide you through.
Prerequisites
What you will need:
- .NET SDK installed on your machine. This article uses 8.0.403
- RavenDB Cloud account and instance with certificate
- Database in RavenDB Cloud
Let’s ensure that we have all the prerequisites to connect your application to the RavenDB. In this tutorial, we will use a RavenDB Cloud instance to start the development in no time.
You can also use your local RavenDB server. You can check how to run the server locally in this guide https://ravendb.net/docs/article-page/csharp/start/getting-started
Let’s setup our development environment. The first step is installing the .NET SDK, which in the case of this tutorial is in version 8.0.403. If you want to check your .NET version, open the terminal and run the following command:
dotnet --version
If you see the .NET version in the output, it means the installation was successful. If this command doesn’t output your version, download the .NET SDK from the link above.
With your .NET environment set up, let’s turn our attention to the RavenDB Cloud. We’ll need a RavenDB Cloud instance and a certificate necessary to encrypt the connection between the database and your application. RavenDB Cloud generates the certificate automatically for your instance. If you’re new to RavenDB Cloud, don’t worry – in this guide, we will also cover how to get started with RavenDB Cloud and how to claim your free instance.
To create your account, start by clicking ‘Get Started Free’(1). If you’d like a free account, simply click ‘Start’(2) without making any changes. Next, select ‘Register Here’. Enter your email address(3) to proceed to the domain name registration step(4). Then, enter the domain name you want, and complete the billing. Review the summary to finish setting up your account. Then you get an activation link on your email address.

We’ve successfully created a RavenDB Cloud account. Now, let’s create a free instance. Instance is like your personal database server. RavenDB Cloud handles setup, maintenance, and other time-consuming tasks to let you focus on the code. To create an instance, click ‘Add Product’ on the right of the ‘Products’ page or just click ‘Add new product to start’ at the center of the page.

If you would like a free instance, don’t change anything – just scroll down and click ‘Next’(1). Enter your account information(2) and then go through billing. Next, you have a ‘Customize’ step(3), where you need to put in the display name of your instance. You also need to set the instance as open to the world (Leave 0.0.0.0/0 if you want it to be open to the world) or delete IP 0.0.0.0/0 and click ‘Add my IP’, to make this only accessible with your IP. You can also change it later so don’t be afraid to delete 0.0.0.0/0. If you want to learn more about CIDR notation, you can read more here. Then, just hit ‘Next’, review the summary and if all looks good, click ‘Create’. For more details on adding a new product, click here.

After configuring your product you will need to wait for the instance to complete the provisioning process before you can access your studio. You will also need a certificate installed.
For a secure connection with RavenDB Studio, let’s get a TLS certificate for your instance. We’ll also use it in our app later. You can download it by going to the ‘Products’ tab on the left bar. Then you select the green button ‘Download Certificate’ on your chosen instance and follow the instructions you get in the pop-up. This pop-up includes a certificate download button and instructions for installing certificates for both Windows and Linux. If you clicked too quickly and got an authentication error, please reload your browser and try again.
Remember to restart your browser after installing the certificate to ensure it recognizes the new certificate.


In order to access RavenDB Studio you require a working certificate. While using RavenDB Cloud, the RavenDB Studio is secured by default – you’ll need a certificate to access the server UI. You’ll also need it to connect to your database within the application code, so this step cannot be skipped. Use your certificate to authorize against RavenDB Studio. Just choose your certificate from the popup window that your browser will display. Once doing so, you should be able to see the UI. Now, let’s create a new database for your application. Select the ‘Databases’ tab and press ‘New database’ to create a new one.

For the purpose of testing, you can also create Sample Data by clicking your database and selecting ‘Tasks’ on the left bar where you can find the option to create it. This data will come in handy later if you want to test the connection between your application and the database.

Setup
We’ve prepared everything you need: a functional .NET environment for building your app and a RavenDB database running on RavenDB Cloud to manage your application’s data. Let’s try to connect a test web application to our instance. First, we will create a new directory for your application file. Use the terminal in your new directory, and execute this command:
dotnet new webapi
Next, we’ll need the RavenDB Client package to communicate with our database within the code. You have to add it to your project by executing the following command:
dotnet add package RavenDB.Client
Let’s take a look inside our brand-new Program.cs file. Inside, you will find some sample code. Let’s cut the unnecessary stuff and only leave the code we need. After clean-up, the code should look like the following:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
//app.UseHttpsRedirection();
app.MapGet("/test", () =>
{
})
.WithName("Test")
.WithOpenApi();
app.Run();
Feel free to just copy this into your file. To finally start coding let’s import required libs. You want to put them at the beginning of your existing code as usual.
using System.Security.Cryptography.X509Certificates;
using Raven.Client;
using Raven.Client.Documents;
Those three allow us to use DocumentStore and x509Certificate. DocumentStore is the base for all RavenDB actions. It communicates with your cluster and can handle multiple databases. It’s important to have a single instance of DocumentStore in place, to maintain a single connection definition to your database. You can learn more about DocumentStore here or here.
To use both of those, first we need to declare a few strings that will allow us to connect into your chosen database. Spin up your DocumentStore by calling documentStore.Initialize() at the bottom to finish off the code. You want to write this code just under the line you commented. Remember to use your own serverURL, databaseName and certificatePath that are connected to your RavenDB database. The comment at the top is there to help you find the place where you need to put your code. It should look like this;
//app.UseHttpsRedirection();
string serverURL = "https://your_RavenDB_server_URL";
string databaseName = "your_database_name";
string certificatePath = @"C:\path_to_your_pfx_file\cert.pfx";
var x509Certificate = new X509Certificate2(certificatePath);
IDocumentStore documentStore = new DocumentStore
{
Urls = new[] { serverURL },
Database = databaseName,
Certificate = x509Certificate
};
documentStore.Initialize();
All this code mixed with an already existing template should work as intended. Right now code should be looking like this:
using System.Security.Cryptography.X509Certificates;
using Raven.Client;
using Raven.Client.Documents;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
//app.UseHttpsRedirection();
string serverURL = "https://your_RavenDB_server_URL";
string databaseName = "your_database_name";
string certificatePath = @"C:\path_to_your_pfx_file\cert.pfx";
var x509Certificate = new X509Certificate2(certificatePath);
IDocumentStore documentStore = new DocumentStore
{
Urls = new[] { serverURL },
Database = databaseName,
Certificate = x509Certificate
};
documentStore.Initialize();
app.MapGet("/test", () =>
{
})
.WithName("Test")
.WithOpenApi();
app.Run();
Let’s check the connection by loading a document from your database in the RavenDB Cloud. For this purpose, we will modify the existing MapGet to create the desired route. This code opens a session for you and loads information about the document with ID employees/1-A and fetches it into our routing map endpoint. Let’s check the connection by loading a document from your database in the RavenDB Cloud.
using (var session = documentStore.OpenSession())
{
var employee = session.Load<Employees>("employees/1-A");
if (employee != null)
{
string title = employee.Title;
string last = employee.LastName;
return $"{title} {last}";
}
return "This Employee does not exist";
}
We will use DocumentSession to interact with document. Session is a basic mechanism that is used to modify, load, create, and keep track of documents. If you wish to learn more about Sessions you can read more in the documentation. In the RavenDB Studio choose one of the documents you want to use. For this example, let’s load a single Employee document and write its details in the console. Add following code at the end of your current one:
class Employees
{
public required string LastName { get; set; }
public required string Title { get; set; }
}
After finishing all of the above steps, your application should look like this
using System.Security.Cryptography.X509Certificates;
using Raven.Client;
using Raven.Client.Documents;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
//app.UseHttpsRedirection();
string serverURL = "https://your_RavenDB_server_URL";
string databaseName = "your_database_name";
string certificatePath = @"C:\path_to_your_pfx_file\cert.pfx";
var x509Certificate = new X509Certificate2(certificatePath);
IDocumentStore documentStore = new DocumentStore
{
Urls = new[] { serverURL },
Database = databaseName,
Certificate = x509Certificate
};
documentStore.Initialize();
app.MapGet("/test", () =>
{
using (var session = documentStore.OpenSession())
{
var employee = session.Load<Employees>("employees/1-A");
if (employee != null)
{
string title = employee.Title;
string last = employee.LastName;
return $"{title} {last}";
}
return "This Employee does not exist";
}
})
.WithName("Test")
.WithOpenApi();
app.Run();
class Employees
{
public required string LastName { get; set; }
public required string Title { get; set; }
}
Now using the terminal type dotnet run and wait a few seconds. This command will display the address where your web application is running. Copy that address, paste it into your browser’s URL bar, and add /test at the end. Once it loads, you should see the output displayed in the top left corner.


As an ending tip, I would like to remind you that if everything is working properly you should delete sample data from your database so it is clean and ready for your data, or just create a new one, dedicated for your app.
Summary
In this tutorial, we explained how to connect your ASP.NET applications to the RavenDB database hosted in the RavenDB Cloud. We used .NET with the RavenDB Client package to connect to our database. We also tested the basic connection using sample data.
Woah, already finished? 🤯
If you found the article interesting, don’t miss a chance to try our database solution – totally for free!