D1 Databases
D1 is Cloudflare’s serverless SQL database, built on SQLite. The integration provides both a hosting side (provision the database) and a client side (query it from your .NET app) — and because D1 is SQLite, the local dev loop runs against a real SQLite file, so the same query code works locally and in production.
Add a database (hosting)
Section titled “Add a database (hosting)”builder.AddCloudflareEnvironment();var catalog = builder.AddD1Database("catalog").RunAsEmulator();await builder.addCloudflareEnvironment();const catalog = await builder.addD1Database('catalog').runAsEmulator();aspire run→ with.RunAsEmulator(), the database is a local SQLite file (created automatically under the AppHost’s.aspire/d1folder). No credentials needed. Without it,aspire runtargets real D1 (and provisions the database).aspire deploy→ the database is always provisioned in real D1 via the Cloudflare API (.RunAsEmulator()only affects run mode).
The default is real D1 — calling .RunAsEmulator() opts into the local SQLite loop, mirroring the RunAsEmulator() convention of Aspire’s Azure integrations.
Options
Section titled “Options”builder.AddD1Database("catalog", databaseName: "catalog-prod") .RunAsEmulator() // back with local SQLite during `aspire run` (no credentials) .WithLocationHint("weur") // D1 primary location hint .AllowDeletion(); // permit `aspire destroy` to delete the databaseawait builder.addD1Database('catalog', { databaseName: 'catalog-prod' }) .runAsEmulator() .withLocationHint('weur') .allowDeletion();Reference it from a service
Section titled “Reference it from a service”builder.AddProject<Projects.Api>("api").WithReference(catalog);await builder.addProject('api', '../Api/Api.csproj').withReference(catalog);This injects the D1 connection string into the service — Provider=Sqlite;Data Source=... locally, or Provider=D1;AccountId=...;Database=...;Token=... in production.
Use it (client)
Section titled “Use it (client)”In the consuming project:
builder.AddD1Client("catalog");This registers an ID1Client bound to the database. SQL uses ? positional placeholders; pass values positionally. The same code runs against SQLite locally and the D1 HTTP API in production:
app.MapPost("/visits", async (ID1Client d1, CancellationToken ct) =>{ await d1.ExecuteAsync("CREATE TABLE IF NOT EXISTS visits (id INTEGER PRIMARY KEY, at TEXT)", parameters: null, ct); await d1.ExecuteAsync("INSERT INTO visits (at) VALUES (?)", [DateTimeOffset.UtcNow.ToString("o")], ct); var count = await d1.QueryFirstOrDefaultAsync<long>("SELECT COUNT(*) FROM visits", parameters: null, ct); return Results.Ok(new { visits = count });});ID1Client maps each row to a POCO (by column name, case-insensitive), a scalar, or a raw D1Row:
public record Product(long Id, string Name, decimal Price);
var products = await d1.QueryAsync<Product>("SELECT id, name, price FROM products WHERE price < ?", [100]);var one = await d1.QueryFirstOrDefaultAsync<Product>("SELECT * FROM products WHERE id = ?", [id]);Run several statements together with BatchAsync (a transaction on SQLite, sequential calls over HTTP):
await d1.BatchAsync([ new D1Statement("INSERT INTO products (name, price) VALUES (?, ?)", "Widget", 9.99m), new D1Statement("INSERT INTO products (name, price) VALUES (?, ?)", "Gadget", 19.99m),]);How the backend is chosen
Section titled “How the backend is chosen”The client reads the Provider from the connection string the AppHost injects:
Provider |
Backend | When |
|---|---|---|
Sqlite |
Microsoft.Data.Sqlite against the local file |
aspire run with .RunAsEmulator() |
D1 |
The D1 HTTP query API | Deployed (or aspire run without the emulator) |
The HTTP backend resolves the database id from its name on first use, so you never hard-code a database id.
Credentials
Section titled “Credentials”- API token (provisioning + runtime HTTP access): needs D1: Edit — see API Tokens. By default the client reuses the environment’s Cloudflare API token; scope it to just D1 with
.WithAccessToken(parameter)on the database if you prefer. - Local dev with
.RunAsEmulator(): none — it’s a plain SQLite file.
