Fix “Unable to Create a DbContext of Type” During EF Core Migrations

Quick answer. Point EF tools at both the migrations project and startup project. If the tools cannot construct the context from application services, provide an intentional design-time factory.

Applies to. EF Core with the provider and version used by your application.

Entity Framework troubleshootingRead inner tooling errorSelect projects and contextVerify design-time creation
Read inner tooling error → Select projects and context → Verify design-time creation

What does the failure look like?

A context lives in a data library, while dependency registrations and configuration are in the web startup project.

How do you diagnose the cause?

Read the inner design-time error. Verify the SDK/tool and runtime packages are compatible, the projects build, the context constructor accepts DbContextOptions<T>, and the startup project supplies the provider and settings. Check environment-specific configuration.

Worked example

From the solution root, identify context, target project, and executable startup project. For a design-time factory, read configuration from an approved source and target the intended development database. Verify context discovery before running migration commands.

CLI
dotnet ef dbcontext info --project src/Data --startup-project src/Web --context AppDbContext

If the preview blocks the Copy button, select the snippet and copy it directly.

How do you verify the fix?

CheckExpected behavior
Repeat the failing operation in development or stagingdbcontext info resolves AppDbContext and reports the intended provider.
Repeat normal traffic and a relevant negative caseValid work completes and the failure path remains observable

These are expected outcomes. The example was not executed against a deployed application.

Frequently asked question

Which project creates the context?

The startup project supplies services and configuration. The target project identifies where tooling creates its output.

Microsoft Learn reference · ASP.NET tutorials

Comments