Skip to content
Latchkey

dotnet ef "No migrations configuration" / Migrations Fail in CI

A dotnet ef migrations / database update / migrations script step failed because EF Core could not build the DbContext at design time - a missing tool, design-time factory, or wrong startup project in CI.

What this error means

The EF step fails with "Unable to create a DbContext", "No project was found", or "No design-time services were found", and no migration runs. It commonly appears when CI lacks the dotnet-ef tool or the context needs runtime config it does not have at design time.

dotnet ef output
Unable to create a 'DbContext' of type 'AppDbContext'. The exception 'Value cannot be
null. (Parameter 'connectionString')' was thrown while attempting to create an instance.

Common causes

dotnet-ef tool not restored in CI

The dotnet ef command is a local/global tool. If dotnet tool restore was not run, the command (or the right version) is missing.

DbContext needs runtime config at design time

The context resolves its connection string from app config/secrets that are not present at design time in CI, so EF cannot instantiate it without a design-time factory.

How to fix it

Restore the EF tool and target the right projects

Restore the local tool and point EF at the correct startup and migrations projects.

Terminal
dotnet tool restore
dotnet ef migrations script --idempotent \
  --project src/Data --startup-project src/Api -o migrate.sql

Add a design-time DbContext factory

Provide an IDesignTimeDbContextFactory so EF can build the context without full runtime config.

C#
public class AppDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
{
    public AppDbContext CreateDbContext(string[] args)
    {
        var options = new DbContextOptionsBuilder<AppDbContext>()
            .UseNpgsql(Environment.GetEnvironmentVariable("ConnectionStrings__Default"))
            .Options;
        return new AppDbContext(options);
    }
}

How to prevent it

  • Run dotnet tool restore before any dotnet ef step in CI.
  • Provide an IDesignTimeDbContextFactory for contexts that need runtime config.
  • Prefer migrations script --idempotent to apply schema changes deterministically in pipelines.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →