Entity Framework Core - deleting child records from parent causes an error in SaveChanges

I get the following error: The connection between the 'Docket' and 'DocketLine' entity types has been disconnected, but the foreign key for this connection cannot be set to null. If the dependent object is to be deleted, then establish a link to use cascading deletes. "

The problem arises because I have a Docket (header) than several children (DocketLines), and I do an update where I add new lines to the docket header, and I just add these new DocketLines to the Docket.DocketLines Collection ( works great). But when I try to remove the DocketLine from the same collection using Docket.DocketLines.Remove (deletedLine), then this generates the error message above. Any idea why?

I had to change my code to remove lines directly from the _context.DocketLines.Remove / RemoveRange (...) collection at the end, and it works, but it seems strange that I will add new elements to chil to insert new DocketLines, but failed remove items from the same collection to remove DocketLines

+5
source share
1 answer

Inside the DbContext / OnModelCreating method, comment on the behavior of the OnDelete object that causes this problem:

enter image description here

This will allow you to avoid this problem.

0
source

All Articles