EF7 RC1 : Disable Cascade Delete

entity-framework-core

Question

In the RC1 of EntityFramework 7, released yesterday, Cascade Delete was added.

To disable it per relationship, I can use :

      builder.Entity<Site>().HasOne(e => e.Person)
      .WithMany(x => x.Sites).Metadata.DeleteBehavior = DeleteBehavior.Restrict;

I want to disable it globally for a DbContext, but I didn't find a way. How can I do ?

Popular Answer

Someone stated on the github project forum that the only way to do it right now is to iterate through all relationships in the method OnModelCreating(ModelBuilder builder), and set the DeleteBehavior property to DeleteBehavior.Restrict :

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



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