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 ?
Cascade delete automatically deletes dependent records or sets null to ForeignKey columns when the parent record is deleted in the database. Cascade delete is enabled by default in Entity Framework for all types of relationships such as one-to-one, one-to-many and many-to-many.
Cascade delete allows the deletion of a row to trigger the deletion of related rows automatically. EF Core covers a closely related concept and implements several different delete behaviors and allows for the configuration of the delete behaviors of individual relationships.
ON DELETE CASCADE : if a row of the referenced table is deleted, then all matching rows in the referencing table are deleted. ON DELETE SET NULL : if a row of the referenced table is deleted, then all referencing columns in all matching rows of the referencing table to be set to null.
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With