Migrating to MVC6 / EF7: PluralizingTableNameConvention

asp.net-core-mvc entity-framework-core

Question

I'm migrating my MVC5 app to MVC6. Currently I'm using two conventions

    public class RentABikeDbContext : DbContext
{
    ...

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    }
}

However it looks like in MVC6/EF7 there is no Conventions property on the new Microsoft.Data.Entity.ModelBuilder class. What is the proper EF7 way to specify conventions?

Accepted Answer

Entity Framework 7 does not have any built-in pluralization, so nothing to remove there, and Cascade delete is not yet implemented https://github.com/aspnet/EntityFramework/issues/333


Popular Answer

Iterate thru all entities, get the foreign keys, and set them to Delete Restricted:

foreach (var relationship in builder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
{
    relationship.DeleteBehavior = DeleteBehavior.Restrict;
}


Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why