{"record":{"id":"7f44a87af7fa3189","repo":"dotnet/efcore","slug":"changes-have-been-made-to-the-model-since-the-last","errorCode":null,"errorMessage":"Changes have been made to the model since the last migration. Add a new migration.","messagePattern":"Changes have been made to the model since the last migration\\. Add a new migration\\.","errorType":"exception","errorClass":"OperationException","httpStatus":null,"severity":"warning","filePath":"src/EFCore.Design/Design/Internal/MigrationsOperations.cs","lineNumber":375,"sourceCode":"\n        return files;\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public virtual void HasPendingModelChanges(string? contextType)\n    {\n        using var context = _contextOperations.CreateContext(contextType);\n\n        var hasPendingModelChanges = context.Database.HasPendingModelChanges();\n\n        if (hasPendingModelChanges)\n        {\n            throw new OperationException(DesignStrings.PendingModelChanges);\n        }\n\n        _reporter.WriteInformation(DesignStrings.NoPendingModelChanges);\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    [RequiresDynamicCode(\"Runtime migration compilation requires dynamic code generation.\")]\n    public virtual MigrationFiles AddAndApplyMigration(\n        string name,\n        string? outputDir,\n        string? contextType,\n        string? @namespace,\n        string? connectionString)","sourceCodeStart":357,"sourceCodeEnd":393,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Design/Internal/MigrationsOperations.cs#L357-L393","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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)."],"exampleFix":"// before: model edited, no new migration\ndotnet ef database has-pending-model-changes\n// Throws\n// after\ndotnet ef migrations add AddProductRating","handlingStrategy":"validation","validationCode":"// Before checking, you can pre-flight the diff yourself\nif (context.Database.HasPendingModelChanges())\n{\n    Console.Error.WriteLine(\"Model has pending changes; run 'dotnet ef migrations add'.\");\n    // exit non-zero instead of letting the command throw\n}","typeGuard":null,"tryCatchPattern":"try { ops.HasPendingModelChanges(contextType); }\ncatch (OperationException ex) when (ex.Message.Contains(\"Changes have been made to the model\"))\n{ /* scaffold a new migration */ }","preventionTips":["Run 'migrations add' immediately after model edits.","Gate CI on a clean model snapshot.","Keep design-time and runtime provider/config in sync."],"tags":["migrations","model-diff","snapshot","design-time"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}