TPT and cascading deletes
EF does not configure "on delete cascade" on foreign keys if the parent is derived from some other class.
I have a Base class, a Derived class and Derived has a collection of Items. I want to configure EF to delete Items when their parent Derived is deleted.
The following minimal (LinqPad) example shows how I'm trying to achieve that, but it doesn't generate the on delete cascade part, just regular FKs. This works without inheritance though.
This issue is also documented with some more details here on SOF: http://stackoverflow.com/questions/31147272/cascading-deletes-with-entity-framework-and-tpt
[System.ComponentModel.DataAnnotations.Schema.Table("Bases")]
public class Base
{
public int Id { get; set; }
public string Name { get; set; }
}
[System.ComponentModel.DataAnnotations.Schema.Table("Derived")]
public class Derived : Base
{
public virtual ICollection<Item> Items { get; set; }
public Derived()
{
Items = new HashSet<Item>();
}
}
public class Item
{
public int Id { get; set; }
public int ParentId { get; set; }
public Derived Parent { get; set; }
}
public class TestDbContext : System.Data.Entity.DbContext
{
public System.Data.Entity.DbSet<Base> Bases { get; set; }
public System.Data.Entity.DbSet<Derived> Derived { get; set; }
public System.Data.Entity.DbSet<Item> Items { get; set; }
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
System.Data.Entity.Database.SetInitializer<TestDbContext>(null);
modelBuilder.Entity<Item>().HasRequired(x => x.Parent).WithMany(x => x.Items).HasForeignKey(x => x.ParentId).WillCascadeOnDelete(true);
}
}
void Main()
{
var ctx = new TestDbContext();
var ddl = (ctx as System.Data.Entity.Infrastructure.IObjectContextAdapter).ObjectContext.CreateDatabaseScript();
Console.WriteLine(ddl);
}
Note for triage: See https://entityframework.codeplex.com/workitem/1501
Note for triage: There are a few different scenarios for which we are not introducing the necessary cascade deletes:
- In the one described in this issue the entities in the TPT hierarchy acts as principal side. E.g. Customer derives from Person and has Orders, if you delete a Customer, the orders won't get deleted, so deleting the customer will fail.
- In the scenario what we got over email recently the TPT hierarchy acts as the dependent side. E.g. School has a collection of Staff and Teacher derives from Staff. If you delete the School and the teachers are not in-memory, the cascade behavior will try to delete the Staff rows but it won't delete the row on the Teachers table so the whole operation will fail.
- A slightly different scenario from (2) which is described in the original CodePlex bug, in which School has a collection of Teacher and Teacher derives from Person but Person is not abstract, i.e. the FK to School is defined in the Teachers table. In this case Cascade deletes are not enough: Deleting a School would cause the rows in Teachers to be deleted but the rows in Person would remain, meaning that the type of each deleted Teacher would mutate to Person. Although this may sound desirable at first glance, it can be quite surprising and violates assumptions that the type is not mutable.
In triage today we discussed the general concern of the potential braking impact of fixing any of these scenarios given that introducing cascade deletes tends to produce cycles which lead to errors in DDL for SQL Server, and about the fact that (3) seems unsolvable with cascade deletes.
I think we should consider each individual scenario separately.
General workaround
It is possible to modify migrations to introduce cascade deletes, like this:
CreateTable(
"dbo.Depedent",
c => new
{
Id = c.Int(nullable: false),
Subject = c.String(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.Principal", t => t.Id, cascadeDelete: true)
.Index(t => t.Id);
Clearing up milestone to discuss in triage.
EF Team Triage: This issue is not something that our team is planning to address in the EF6.x code base. This does not mean that we would not consider a community contribution to address this issue.
Moving forwards, our team will be fixing bugs, implementing small improvements, and accepting community contributions to the EF6.x code base. Larger feature work and innovation will happen in the EF Core code base (https://github.com/aspnet/EntityFramework).
Closing an issue in the EF6.x project does not exclude us addressing it in EF Core. In fact, a number of popular feature requests for EF have already been implemented in EF Core (alternate keys, batching in SaveChanges, etc.).
BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.
For anyone arriving here: EF Core does not (yet) support TPT mapping, see https://github.com/aspnet/EntityFrameworkCore/issues/2266