Fix For Entity Framework Core Not Generating Indexes & Constraints
[Entity Framework Core, Fixes]
If you are using Entity Framework Core, and are configuring your models using configuration classes (as you probably should) you might run into a situation where after adding your migrations and updating your database, you find that the resulting schema does not have indexes or unique constraints.
Should you run into this problem check the following
You Are Overriding OnModelCreating
On your DbContext
class, make sure that you are overriding OnModelCreating()
, and within that method you are applying the configurations.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(typeof(YOURDATABSEContext).Assembly);
}
The ApplyConfigurationsFromAssembly
method here scans the assembly containing the DBContext
passed and applies all the configurations it finds.
You Are Correctly Implementing IEntityTypeConfiguration
Make sure that you are correctly implementing IEntityTypeConfiguration<TEntity>
and calling the Configure
method.
I spent some hours trying to figure out why this configuration was being skipped.
Can you spot the problem here?
I had inadvertently omitted the crucial bit highlighted here.
If you omit that bit Entity Framework Core will not be able to find and apply your configurations.
Your code should now behave as expected.
Happy hacking!