Skip to content

Use Dapper and ADO.NET with .NET ​

The .NET SDK ships an ADO.NET provider: TerraBaseDbConnection wraps a TerraBaseSqlDatabase in a DbConnection, so every .NET tool built on ADO.NET works against your TerraScale database. Dapper accepts that connection directly. The SQL dialect is SQLite; everything the SQL API refuses (see SQL) is refused through the provider too.

Install ​

The .NET SDK is not on NuGet yet; follow the C# quickstart to pack it into a local package folder, then add Dapper to your project:

bash
dotnet add package TerraScale.DotNet --source ~/.nuget/packages-terrascale
dotnet add package Dapper

Connect ​

Export your project ID and API key as in the C# quickstart, then build the client and wrap the database handle:

csharp
using TerraScale.DotNet;
using TerraScale.DotNet.TerraBase;

using var http = new HttpClient(TerraBaseHttp.CreateHandler());
var sql = new TerraBaseSqlClient(http, new TerraBaseSqlClientOptions
{
    ApiKey = Environment.GetEnvironmentVariable("TERRASCALE_API_KEY"),
    Region = Environment.GetEnvironmentVariable("TERRASCALE_REGION"),
});
TerraBaseSqlDatabase db = sql.Database(Environment.GetEnvironmentVariable("TERRASCALE_PROJECT_ID")!, "main");
TerraScaleIdGenerator ids = new();

await using var connection = new TerraBaseDbConnection(db);
await connection.OpenAsync();

The connection has no connection string: it is built from the database handle, so the key never appears in a string. Outside a transaction, each command is its own request and commits on its own.

Dapper ​

Dapper's QueryAsync maps rows to your types and ExecuteAsync returns the rows a write changed. Parameters are named (@name, :name or $name) or positional (?), and values are sent as typed parameters, never interpolated text:

csharp
using Dapper;

public sealed record Person(string Id, string Name);

var people = await connection.QueryAsync<Person>(
    "SELECT id, name FROM people WHERE name = @name", new { name = "Ada" });

var inserted = await connection.ExecuteAsync(
    "INSERT INTO people (id, name) VALUES (@id, @name)", new { id = ids.NewId(), name = "Ada" });

Blobs bind to byte[] parameters, DateTime values travel as UTC text, and integers arrive as exact 64-bit numbers.

Plain ADO.NET ​

Without Dapper, commands and readers are the standard ADO.NET types:

csharp
await using var command = connection.CreateCommand();
command.CommandText = "SELECT count(*) AS n FROM people";
await using var reader = await command.ExecuteReaderAsync();
reader.Read();
long count = reader.GetInt64(0);

Transactions and limits ​

BeginTransactionAsync opens a transaction over the SDK's SQL WebSocket session; commit and rollback close it, and every command in between runs in that one server-side transaction. Pass the transaction to Dapper with transaction::

csharp
await using (var tx = await connection.BeginTransactionAsync())
{
    await connection.ExecuteAsync(
        "INSERT INTO people (id, name) VALUES (@id, @name)",
        new { id = ids.NewId(), name = "Grace" }, transaction: tx);
    await connection.ExecuteAsync(
        "INSERT INTO people (id, name) VALUES (@id, @name)",
        new { id = ids.NewId(), name = "Linus" }, transaction: tx);
    await tx.CommitAsync();
}

Dispose without a commit rolls the transaction back, and so does closing or disposing the connection asynchronously; a sync Dispose detaches the transaction instead, and the server rolls it back at the limit below. A server-side transaction lives at most 5 seconds (see SQL); hold it only for the statements that must commit together. Only Serializable isolation exists, matching the strongest isolation the database provides.

While a transaction is open, every command on that connection must enlist in it: set command.Transaction (Dapper does this from its transaction: argument) or the command throws.

CommandTimeout defaults to 30 seconds and bounds each command on the client: a command still running then throws TimeoutException. 0 waits up to the SDK's 10-minute request limit. Raising it never extends server execution: TerraBase stops a single query at the plan's query timeout (see limits), so keep the default unless a command legitimately queues behind others. Dapper sets it per call with commandTimeout:.

Async only ​

The provider is async-only, like the rest of the SDK: the sync ExecuteNonQuery, ExecuteScalar, ExecuteReader, BeginTransaction, Commit and Rollback throw NotSupportedException. Use the async overloads and pass a CancellationToken.

What isn't supported ​

  • EF Core does not run on this ADO.NET provider. Use EF Core over the Postgres wire instead (below).
  • BEGIN/COMMIT/ROLLBACK statements are refused; use the transaction API above.
  • Server-side cursors and prepared-statement handles do not exist; commands send their SQL text each time.

EF Core over the Postgres wire ​

EF Core works with Npgsql.EntityFrameworkCore.PostgreSQL against the Postgres wire, including dotnet ef migrations and dotnet ef database update:

cs
builder.Services.AddDbContext<AppDbContext>(options => options.UseNpgsql(new NpgsqlConnectionStringBuilder
{
    Host = "pg.terrascale.tech",
    Port = 5432,
    Database = $"{projectId}/{database}",
    Username = "any",
    Password = apiKey,
    SslMode = SslMode.VerifyFull,
}.ConnectionString));

await context.Database.BeginTransactionAsync(IsolationLevel.Serializable) runs the transaction in strong mode; a lost race throws a PostgresException with SQLSTATE 40001, which EF Core's EnableRetryOnFailure retries.