Entity Framework Migration Error Fix
In this tutorial, you'll learn about Entity Framework Migration Error Fix. We cover key concepts, practical examples, and best practices.
Running dotnet ef migrations add fails with "An error occurred while accessing the Microsoft.Extensions.Hosting services":
Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=2122456
The EF Core migration tool cannot instantiate your DbContext because it lacks a parameterless constructor or the hosting configuration is missing. This happens when your DbContext requires dependency injection parameters that the design-time factory cannot resolve.
Step-by-Step Fix
1. Create an IDesignTimeDbContextFactory
WRONG — DbContext with only parameterized constructor:
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
}
RIGHT — add a design-time factory:
public class AppDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
{
public AppDbContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>();
optionsBuilder.UseSqlServer(
"Server=(localdb)\\mssqllocaldb;Database=MyApp;Trusted_Connection=True");
return new AppDbContext(optionsBuilder.Options);
}
}
Place this in the same project as your DbContext.
2. Fix snapshot mismatch after manual deletion
WRONG — deleting migration files manually:
rm Migrations/20240101000000_InitialCreate.cs
RIGHT — revert using the migration tool:
dotnet ef migrations remove
Or roll back and delete properly:
dotnet ef database update 0
dotnet ef migrations remove
3. Handle pending changes detection
WRONG — adding a migration when model snapshot is out of sync:
dotnet ef migrations add AddCategoryTable
Error: "Your target project doesn't match your migrations assembly."
RIGHT — specify the correct project:
dotnet ef migrations add AddCategoryTable \
--project src/MyApp.Data \
--startup-project src/MyApp.Web
4. Fix foreign key constraint during update
WRONG — adding a column with a foreign key to a table with data:
dotnet ef database update
# Error: ALTER TABLE ADD COLUMN with non-nullable FK requires a default
RIGHT — make it nullable or provide a default:
builder.Property<int?>("CategoryId");
Or add a default SQL value:
builder.Property<int>("CategoryId").HasDefaultValue(1);
5. Regenerate from scratch
When migrations are irrecoverably broken:
dotnet ef migrations remove
dotnet ef migrations add InitialCreate
dotnet ef database update
Expected output:
Build succeeded.
Done. To undo this action, use 'ef migrations remove'.
Applying migration '20240624_InitialCreate'.
Done.
Prevention
- Never manually delete migration files — always use
dotnet ef migrations remove. - Add an
IDesignTimeDbContextFactory<T>in every project with migrations. - Keep migrations in a separate class library project.
- Run
dotnet ef migrations listbefore adding new migrations. - Commit migration files and the ModelSnapshot to source control.
Common Mistakes with framework migration
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations
These mistakes appear frequently in real-world ENTITY code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro