I have DB model with table per type inheritance. For example, entities are A, B, C, A1, A2. Base - A Derived - A1, A2. Another - B, C. So, A has 1 to 1 association to A1 and A2. B and C has associations(1 to many, with OnDelete action on the DB side) to A1 and A2 respectively.
Problem
I trying to delete record from B, so I expect that EF remove also all A1 objects which associated to current B's record.
In the end, EF remove record from B and all associated records from A1, but not from A
Why? how fix it?
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.
The EF Core in-memory database does not currently support cascade deletes in the database.
None. No records are deleted and the foreign key column is not cleared. This is the default setting.
The DbContext class has a method called OnModelCreating that takes an instance of ModelBuilder as a parameter. This method is called by the framework when your context is first created to build the model and its mappings in memory.
Note: EF automatically deletes related records in the middle table for many-to-many relationship entities if one or the other entity is deleted. Thus, EF enables the cascading delete effect by default for all the entities. Use Fluent API to configure entities to turn off cascade delete using the WillCascadeOnDelete () method, as shown below.
The EF Core in-memory database does not currently support cascade deletes in the database. Do not configure cascade delete in the database when soft-deleting entities. This may cause entities to be accidentally really deleted instead of soft-deleted.
There are three approaches to represent an inheritance hierarchy in Entity Framework; Table-per-type inheritance uses a separate table in the database to maintain data for non-inherited properties and key properties for each type in the inheritance hierarchy.
Do not configure cascade delete in the database when soft-deleting entities. This may cause entities to be accidentally really deleted instead of soft-deleted. Some databases, most notably SQL Server, have limitations on the cascade behaviors that form cycles.
It's a known problem and I would call it a bug. Obviously only deleting the records from the table A1
for the derived entity's properties cannot be correct. The remaining data in the database (in table A
) do represent another object type. In other words: This DELETE didn't actually delete an entity but it changed the entity's type = transformed an object of type A1
into an object of type A
- which makes even less sense if A
is an abstract entity.
The recommended workaround from here (as I understand it) is ugly:
var b = context.Bs.Include("A1s").Single(b => b.Id == 1);
foreach (var a1 in b.A1s.ToList())
context.As.Remove(a1);
context.Bs.Remove(b);
context.SaveChanges();
context.As.Remove(a1);
should delete from both A
and A1
table, thereby fixing the problem of the orphaned records in table A
. Unfortunately you are forced to load the children from the database to delete the parent correctly.
Here is another question and answer about this problem: Problems using TPT (Table Per Type) in EF 4.2 and deletion of parent objects
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