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?

1
2
8/2/2015 1:49:06 PM

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

2
8/2/2015 3:46:14 PM

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 Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow