dotnet/efcore · warning · OperationException
Changes have been made to the model since the last…
Error message
Changes have been made to the model since the last migration. Add a new migration.
What it means
HasPendingModelChanges instantiates the context and asks the database provider whether the current model differs from the last migration's target model. If the snapshot-based diff reports pending changes, EF throws to signal that 'dotnet ef migrations add' must be run before further migrations work. This is the command backing the 'has-pending-model-changes' diagnostic, not an automatic gate inside AddMigration.
Solutions
- Add a new migration: 'dotnet ef migrations add <Name>' so the model snapshot and the latest migration target model agree.
- If the diff is spurious (provider/resolver differences across environments), regenerate the model snapshot by adding then removing an empty migration.
- Ensure the context configuration at design time matches runtime (same provider, same OnModelCreating).
Example fix
// before: model edited, no new migration dotnet ef database has-pending-model-changes // Throws // after dotnet ef migrations add AddProductRating
Defensive patterns
Strategy: validation
Validate before calling
// Before checking, you can pre-flight the diff yourself
if (context.Database.HasPendingModelChanges())
{
Console.Error.WriteLine("Model has pending changes; run 'dotnet ef migrations add'.");
// exit non-zero instead of letting the command throw
} Try / catch
try { ops.HasPendingModelChanges(contextType); }
catch (OperationException ex) when (ex.Message.Contains("Changes have been made to the model"))
{ /* scaffold a new migration */ } Prevention
- Run 'migrations add' immediately after model edits.
- Gate CI on a clean model snapshot.
- Keep design-time and runtime provider/config in sync.
When it happens
Trigger: Invoking MigrationsOperations.HasPendingModelChanges(contextType) (the EF tool 'database has-pending-model-changes' / equivalent) after the model has been edited (entity added/removed, property changed, etc.) without a corresponding new migration. context.Database.HasPendingModelChanges() returns true.
Common situations: Editing an entity or DbContext configuration then running the pending-changes check in CI or a pre-commit hook before scaffolding a migration; switching provider or adding an index/column; reverting a migration's model changes but not its files.
Related errors
- No DbContext was found in assembly
- The project language
- The requested configuration is not stored in the…
- A migration name must be specified.
- A seed entity for entity type
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/7f44a87af7fa3189.
Report an issue: GitHub.
Appendix: source
Thrown at src/EFCore.Design/Design/Internal/MigrationsOperations.cs:375
return files;
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual void HasPendingModelChanges(string? contextType)
{
using var context = _contextOperations.CreateContext(contextType);
var hasPendingModelChanges = context.Database.HasPendingModelChanges();
if (hasPendingModelChanges)
{
throw new OperationException(DesignStrings.PendingModelChanges);
}
_reporter.WriteInformation(DesignStrings.NoPendingModelChanges);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
[RequiresDynamicCode("Runtime migration compilation requires dynamic code generation.")]
public virtual MigrationFiles AddAndApplyMigration(
string name,
string? outputDir,
string? contextType,
string? @namespace,
string? connectionString)View on GitHub (pinned to 3a2006ef56)