0

According to Enabling Cascade Delete on Microsoft's web site:

You can remove these cascade delete conventions by using:

modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>()
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>()

The following code configures the relationship to be required and then disables cascade delete.

C#

modelBuilder.Entity<Course>()
    .HasRequired(t => t.Department)
    .WithMany(t => t.Courses)
    .HasForeignKey(d => d.DepartmentID)
    .WillCascadeOnDelete(false);

So, as far as I understood, Remove<OneToManyCascadeDeleteConvention>() removes cascade delete for all entities in this context, while WillCascadeOnDelete(false) only removes only the related entity (Course entity in the example above). Is that true?

  • The property refers to children. See : https://stackoverflow.com/questions/17932720/how-does-willcascadeondelete-in-entity-framework-work – jdweng Sep 03 '19 at 11:26
  • @jdweng Sorry, I could not understand, could you pls explain a little bit more? On the other hand, **Remove() removes cascade delete for all entities in this context, while WillCascadeOnDelete(false) only removes only the related entity (Course entity in the example above). Is that true?** –  Sep 03 '19 at 11:28
  • Any help please? –  Sep 03 '19 at 11:33
  • 1
    "Is that true?" Yes. EF appllies a set of conventions when building the model, and you can override the conventions with the fluent configuration or attributes. Removing a convention applies to all your entities. – David Browne - Microsoft Sep 03 '19 at 12:11
  • @DavidBrowne-Microsoft Many thanks David. Do you suggest to use `Remove()` in general situation? And when using `Remove()`, can I also use **nullable Foreign Key** in order to set child record's parent Id's = null? –  Sep 03 '19 at 12:23
  • Any help please? –  Sep 03 '19 at 12:43
  • Common practice is to leave the conventions and override using Fluent configuration as desired. – David Browne - Microsoft Sep 03 '19 at 13:07

0 Answers0