Getting started with the .NET client
The .NET client lets a C# application talk to a RasterizedDB server over its native protocol: an ADO.NET driver (RasterizedDb.Data) and an Entity Framework Core provider built on it (RasterizedDb.EntityFrameworkCore). Use it when your application is written in .NET and you want the server's fast native path instead of the PostgreSQL wire protocol.
Supported .NET versions
| package | target frameworks | depends on |
|---|---|---|
RasterizedDb.Data | net8.0, net9.0, net10.0 | BouncyCastle.Cryptography 2.6.2 |
RasterizedDb.EntityFrameworkCore | net8.0, net9.0, net10.0 | RasterizedDb.Data; Microsoft.EntityFrameworkCore.Relational 10.0.5 on .NET 10, 9.0.10 on .NET 8 and 9 |
RasterizedDb.Messaging | net8.0, net9.0, net10.0 | System.IO.Hashing 8.0.0 |
So the EF Core provider is an EF Core 10 provider on .NET 10 and an EF Core 9 provider on .NET 8 and .NET 9. The packages are on nuget.org at version 1.1.0.
RasterizedDb.Messaging is a client for the server's message broker. It is pre-production: the broker protocol it speaks is not frozen. See Messaging.
Install the packages
Add the driver from NuGet:
dotnet new console -o HelloRdb
cd HelloRdb
dotnet add package RasterizedDb.Data --version 1.1.0For EF Core, add the provider instead (it brings RasterizedDb.Data with it):
dotnet add package RasterizedDb.EntityFrameworkCore --version 1.1.0For the message broker client:
dotnet add package RasterizedDb.Messaging --version 1.1.0Or build them from source
The packages are in the dotnet/ folder of the RasterizedDB repository. You need the .NET 10 SDK to build them; the result runs on .NET 8, 9 and 10. Clone the release tag next to your application and add a project reference, from the folder that holds HelloRdb:
git clone --branch v1.1.0 https://github.com/milen-denev/rasterizeddb.git
cd HelloRdb
dotnet add reference ../rasterizeddb/dotnet/src/RasterizedDb.Data/RasterizedDb.Data.csprojThe EF Core provider is dotnet/src/RasterizedDb.EntityFrameworkCore/RasterizedDb.EntityFrameworkCore.csproj and the broker client dotnet/src/RasterizedDb.Messaging/RasterizedDb.Messaging.csproj.
Start a server to connect to
The driver needs a running server with its native listener, which is the default. Install the server as described in Installing, then start it with a data directory and a first role:
export RASTERIZEDDB_PASSWORD='change-me-now'
rasterizeddb --location ./rdb-data --rdb-user appThis listens on 127.0.0.1:61170 (the native listener's default address and port) and asks for a password (SCRAM-SHA-256). On a data directory with no roles yet, it creates the role app with the password from RASTERIZEDDB_PASSWORD. See Running the server for the other flags.
A first program
Replace Program.cs in the HelloRdb project with:
using RasterizedDb.Data;
var connectionString = "Host=127.0.0.1;Port=61170;Username=app;Password=change-me-now";
// One data source per connection string, kept for the life of the application: it owns the pool.
await using var dataSource = new RasterizedDbDataSource(connectionString);
await using var connection = await dataSource.OpenConnectionAsync();
await using (var create = connection.CreateCommand(
"CREATE TABLE IF NOT EXISTS greetings (id BIGSERIAL PRIMARY KEY, message TEXT NOT NULL)"))
{
await create.ExecuteNonQueryAsync();
}
await using (var insert = connection.CreateCommand(
"INSERT INTO greetings (message) VALUES (@message) RETURNING id"))
{
insert.Parameters.Add("message", "Hello from .NET");
var id = Convert.ToInt64(await insert.ExecuteScalarAsync());
Console.WriteLine($"inserted row {id}");
}
await using (var select = connection.CreateCommand("SELECT id, message FROM greetings ORDER BY id"))
await using (var reader = await select.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
Console.WriteLine($"{reader.GetInt64(0)}: {reader.GetString(1)}");
}
}Run it:
dotnet runOn a fresh data directory it prints:
inserted row 1
1: Hello from .NETWhat happened:
- The connection string names the server and the login. Every keyword is listed in Connecting.
- Over TCP the connection is always TLS 1.3. With no other setting the driver accepts the server's certificate without checking it, which suits a server on your own machine; read TLS before you connect over a network you do not control.
@messageis a parameter. Its value is written into the statement as a safely quoted SQL literal; see Commands and parameters.- The rows came back in the text result format, the default, which carries column names. The faster binary format is described in Reading results.
The same with EF Core
using Microsoft.EntityFrameworkCore;
var options = new DbContextOptionsBuilder<GreetingsContext>()
.UseRasterizedDb("Host=127.0.0.1;Port=61170;Username=app;Password=change-me-now")
.Options;
await using var context = new GreetingsContext(options);
await context.Database.EnsureCreatedAsync();
context.Greetings.Add(new Greeting { Message = "Hello from EF Core" });
await context.SaveChangesAsync();
foreach (var greeting in await context.Greetings.OrderBy(g => g.Id).ToListAsync())
{
Console.WriteLine($"{greeting.Id}: {greeting.Message}");
}
public class Greeting
{
public long Id { get; set; }
public string Message { get; set; } = "";
}
public class GreetingsContext(DbContextOptions<GreetingsContext> options) : DbContext(options)
{
public DbSet<Greeting> Greetings => Set<Greeting>();
}EnsureCreatedAsync creates the tables only when the database has no tables at all, so run this against a fresh data directory, not the one the first program wrote to. Real applications use migrations instead. Migrations, LINQ translation and type mapping are covered in EF Core.
What else is in RasterizedDb.Data
Besides the ADO.NET classes, a RasterizedDbConnection gives typed access to two more parts of the server:
connection.Admin: the server's administration statements as calls (TablesAsync,CreateIndexAsync,CreateRoleAsync, and more). See Administration.connection.Kv: the server's key/value cache (SetAsync,GetStringAsync,IncrementAsync, typed maps). See Key/value cache.
Every call on both has a synchronous twin without the Async suffix.
Next steps
- Connecting: connection strings, TLS, unix sockets, pooling.
- Commands and parameters: running statements, batches, cancellation.
- Reading results: data readers and the type mapping.
- Transactions: isolation, savepoints and retrying conflicts.
- Administration:
connection.Admin, the server's administration statements as typed calls. - Key/value cache:
connection.Kv, typed maps and object repositories. - Messaging: queues, topics and exchanges with
RasterizedDb.Messaging. - EF Core: the Entity Framework Core provider.
- Limits: what the driver does not do yet, and when to use Npgsql instead.