Operations
| Operation | Description |
|---|---|
Ef.DbContextFrom | Create a reference to a DbContext in a project. |
Ef.DatabaseUpdate | Apply pending EF Core migrations to the database. Can accept a connection string or an OutputRef from a Bicep deployment. |
Ef.Script | Generate an idempotent SQL migration script ( dotnet ef migrations script --idempotent [from] [to]). Signatures: Script(EfContextRef context, BuildPath outputFile, string? fromMigration = null, string? toMigration = null) and Script(EfContextRef context, BuildPath outputFile, Action<EfScriptOptions> configure), where the options have From(string), From(Func<string?>), To(string) and To(Func<string?>). The from and to migrations are positional arguments of dotnet ef. With toMigration set and no fromMigration, ANDO passes 0 (the empty database) as the start. The Func forms of the options resolve the migrations when the step runs, so they can use the Value of an earlier step; a null or empty result means not set. |
Operation Details
Ef.DbContextFrom
source
Create a reference to a DbContext in a project.
var db = Ef.DbContextFrom(DataProject);
var db = Ef.DbContextFrom(DataProject, "AppDbContext");Ef.DatabaseUpdate
source
Apply pending EF Core migrations to the database. Can accept a connection string or an OutputRef from a Bicep deployment.
Ef.DatabaseUpdate(db);
Ef.DatabaseUpdate(db, connectionString: "Server=...");
Ef.DatabaseUpdate(db, deployment.Output("sqlConnectionString"));Ef.Script
source
Generate an idempotent SQL migration script (dotnet ef migrations script --idempotent [from] [to]). Signatures: Script(EfContextRef context, BuildPath outputFile, string? fromMigration = null, string? toMigration = null) and Script(EfContextRef context, BuildPath outputFile, Action<EfScriptOptions> configure), where the options have From(string), From(Func<string?>), To(string) and To(Func<string?>). The from and to migrations are positional arguments of dotnet ef. With toMigration set and no fromMigration, ANDO passes 0 (the empty database) as the start. The Func forms of the options resolve the migrations when the step runs, so they can use the Value of an earlier step; a null or empty result means not set.
Ef.Script(db, Root / "migration.sql");
Ef.Script(db, Root / "migration.sql", fromMigration: "Init");
Ef.Script(db, Root / "migration.sql", "Init", "AddOrders");
Ef.Script(db, Root / "migration.sql", o => o.From(() => applied.Value).To(() => latest.Value));Setup
EF Core operations require the dotnet-ef tool. ANDO can install it automatically.
// Define the EF tool (installs if needed)
var EfTool = Dotnet.Tool("dotnet-ef", "9.0.0");
Example
Run migrations and generate SQL scripts.
// Define the DbContext reference
var DataProject = Dotnet.Project("./src/Data/Data.csproj");
var db = Ef.DbContextFrom(DataProject, "AppDbContext");
// Apply migrations to the database
Ef.DatabaseUpdate(db);
// Or generate an idempotent SQL script
Ef.Script(db, Root / "artifacts" / "migration.sql");
Operations Reference
Ef.DbContextFrom
Creates a reference to an EF Core DbContext for use in other operations.
| Parameter | Description |
|---|---|
project | ProjectRef pointing to the project containing the DbContext. |
contextName | Name of the DbContext class (e.g., “AppDbContext”). If the project has only one DbContext, this can be omitted. |
Ef.DatabaseUpdate
Applies all pending migrations to the database.
| Parameter | Description |
|---|---|
context | DbContext reference from Ef.DbContextFrom(). |
connectionString | Optional connection string. If not provided, uses the connection string from the DbContext’s configuration. |
Ef.Script
Generates an idempotent SQL script for all migrations.
| Parameter | Description |
|---|---|
context | DbContext reference from Ef.DbContextFrom(). |
outputFile | Path where the SQL script will be written. |
fromMigration | Optional starting migration. Only generates SQL for migrations after this point. Also available as o.From(string) / o.From(Func<string?>) in the options form below (the function runs when the step runs; null or empty means not set). |
toMigration | Optional last migration to include. Also o.To(string) / o.To(Func<string?>). With toMigration set and no fromMigration, ANDO passes 0 (the empty database) as the start, because dotnet ef migrations script [from] [to] is positional. |
Ef.Script(db, Root / "up-to-orders.sql", toMigration: "AddOrders");
Ef.Script(db, Root / "delta.sql", o => o.From(() => applied.Value).To(() => latest.Value));