What Causes Schema Drift
Schema drift happens when the actual database schema diverges from what the migration history says it should be — caused by a developer manually altering a table in SQL Server Management Studio, an emergency hotfix applied directly to production during an incident, or two developers each adding a migration from the same parent snapshot and merging their branches without regenerating a unified migration. Because migrations add diffs the current model against the ModelSnapshot, not the live database, drift is invisible to EF Core until a migration fails to apply or produces incorrect SQL against a database that doesn't match what history claims.
Cricket analogy: It's like a groundsman quietly relaying a section of pitch overnight without updating the official pitch report — the next day's match officials work from a report that no longer matches reality.
Detecting Drift
Comparing the output of dotnet ef migrations script against the actual database schema using a schema-diff tool can reveal discrepancies, but a more reliable habit for teams is running dotnet ef migrations has-pending-model-changes — available from EF Core 8 onward — as a CI step that fails the build whenever the current model and the latest migration are out of sync, catching the problem before it ever reaches a shared database.
Cricket analogy: It's like a match referee cross-checking the official pitch report against an independent groundsman's inspection before play starts, catching any unauthorized relaying before the first ball is bowled.
# CI step (GitHub Actions) that fails the build if the model and migrations are out of sync
- name: Check for pending model changes
run: dotnet ef migrations has-pending-model-changes --project src/MyApp.Data
# Exits non-zero if OnModelCreating changes weren't captured in a migrationResolving Merge Conflicts in Migrations
When two branches each add a migration against the same parent ModelSnapshot, merging both introduces duplicate or conflicting edits to ModelSnapshot.cs. The standard fix is to remove one branch's migration with dotnet ef migrations remove, rebase that branch onto the other branch's now-merged migration, and regenerate a fresh migration from the updated baseline — producing a single linear history with one consistent snapshot state rather than two competing ones.
Cricket analogy: It's like two separate committees independently amending the same rulebook clause from the same starting edition — the fix is to accept one committee's amendment as official, then have the second committee redraft theirs against that new baseline.
Never manually hand-edit ModelSnapshot.cs to resolve a merge conflict. Because it's a generated, self-consistent serialization of the entire model, hand-edited inconsistencies are extremely difficult to spot and can cause silent, incorrect diffs in future migrations. Always resolve conflicts by removing and regenerating migrations, letting the EF Core tooling produce a fresh, consistent snapshot.
- Schema drift is a mismatch between the real database schema and what the migration history/ModelSnapshot says it should be.
- Manual database changes and unmerged parallel migrations are the two most common causes of drift.
- Drift is invisible to EF Core because migrations diff against the snapshot, not the live database.
dotnet ef migrations has-pending-model-changes(EF Core 8+) can catch model/migration mismatches in CI.- Merge conflicts in migrations should be resolved by removing and regenerating, never by hand-editing ModelSnapshot.cs.
- A single linear migration history with one consistent snapshot state is essential for team collaboration.
Practice what you learned
1. What is schema drift?
2. Why is schema drift invisible to EF Core until it causes a failure?
3. What command, available from EF Core 8, helps catch model/migration mismatches in CI?
4. What is the recommended way to resolve a merge conflict in ModelSnapshot.cs?
Was this page helpful?
You May Also Like
Migrations Fundamentals
Learn how EF Core migrations track model changes and generate versioned database schema updates while preserving existing data.
Applying and Reverting Migrations
How to apply pending migrations to a database, generate deployable SQL scripts, and safely roll back schema changes.
Migrations in CI/CD Pipelines
Patterns for safely automating EF Core migration deployment within continuous integration and delivery pipelines, including zero-downtime strategies.
Related Reading
Related Study Notes in Microsoft Technologies
Browse all study notesWindows 10 / UWP Development Study Notes
.NET · 30 topics
Microsoft TechnologiesWindows Batch Scripting Study Notes
Batch · 30 topics
Microsoft TechnologiesMFC (Microsoft Foundation Classes) Study Notes
C++ · 30 topics
Microsoft TechnologiesSilverlight Study Notes
.NET · 30 topics
Microsoft TechnologiesXAML Study Notes
.NET · 30 topics
Microsoft TechnologiesWPF (Windows Presentation Foundation) Study Notes
.NET · 30 topics