Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cascade delete in entity framework ( table per type inheritance )

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?

like image 614
Developex Avatar asked Jan 30 '12 13:01

Developex


People also ask

Does Entity Framework cascade delete?

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.

Will Cascade delete Entity Framework Core?

The EF Core in-memory database does not currently support cascade deletes in the database.

What is the default value of cascade delete?

None. No records are deleted and the foreign key column is not cleared. This is the default setting.

What is OnModelCreating in Entity Framework?

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.

How do I Turn Off Cascade delete in Entity Framework (EF)?

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.

Does the EF Core in-memory database support Cascade deletes?

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.

What is inheritance hierarchy in Entity Framework?

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?

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.


1 Answers

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

like image 108
Slauma Avatar answered Sep 28 '22 06:09

Slauma