Skip to content

Instantly share code, notes, and snippets.

@bobdesaunois
Created April 30, 2019 11:23
Show Gist options
  • Save bobdesaunois/9610681c11819184ac1c62492f1b7ff6 to your computer and use it in GitHub Desktop.
Save bobdesaunois/9610681c11819184ac1c62492f1b7ff6 to your computer and use it in GitHub Desktop.
Disable Cascade deleting globally (Put this in OnModelCreating)
var cascadeFKs = modelBuilder.Model.GetEntityTypes()
.SelectMany(t => t.GetForeignKeys())
.Where(fk => !fk.IsOwnership && fk.DeleteBehavior == DeleteBehavior.Cascade);
foreach (var fk in cascadeFKs)
fk.DeleteBehavior = DeleteBehavior.Restrict;
base.OnModelCreating(modelBuilder);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment