9

I'm using EF with Code first and TPT (Table per Type) inheritance. I have the following model:

public partial class AccountHolder
{
    public int AccountHolderId { get; set; }

    public virtual Address Detail { get; set; }  
    public virtual Nominee Nominee { get; set; }   
}

public partial class Nominee
{
    public int NomineeId { get; set; }             
}

public abstract class Address
{
    public int AddressId { get; set; }
    ...
}

public class PersonalDetail : Address
{
    public int PersonalDetailId { get; set; }
    ...
}

Fluent api :

        modelBuilder.Entity<AccountHolder>().HasOptional(p => p.Nominee)
                                            .WithRequired()
                                            .WillCascadeOnDelete();

According to this tutorial here is a polymorphic relationship between AccountHolder and Address. PersonalDetail inherit Address. My issue is whenever i delete any AccountHolder i want that EF will take care of deleting its associated PersonalDetail information from Address and PersonalDetail table but currently this is not happing, can anybody please guid me how i can implement this behavior through fluent API or some other approach ?

Edit:

According to the posted answer when i am using this configuration :

        modelBuilder.Entity<AccountHolder>().HasOptional(p => p.Detail)
                                            .WithRequired()
                                            .WillCascadeOnDelete();

for enabling cascade delete that this is conflicting with existing 1 to 1 association between AccountHolder and Nominee. The exception is :

Conflicting changes detected. This may happen when trying to insert multiple entities with the same key.

Gaurav
  • 8,367
  • 14
  • 55
  • 90

1 Answers1

1

Check out the WillCascadeOnDelete function when specifying the EntityTypeConfiguration

http://msdn.microsoft.com/en-us/library/gg679348(v=vs.103).aspx

In that example you posted in the question, they have:

modelBuilder.Entity<Customer>()
              .HasOptional(c => c.BillingAddress)
              .WithRequired();

Slap a WillCascadeOnDelete(true) at the end of that (or for whatever Entity you want to have cascade), and that should do it. Something like:

modelBuilder.Entity<AccountHolder>().HasOptional(a => a.Address).WithRequired().WillCascadeOnDelete(true);
Stephen Fischer
  • 2,445
  • 2
  • 23
  • 38
  • I have already a 1 to 1 relationship between `AccountHolder and OtherTable` with the same configuration which you suggested so thats why when i am adding the configuration which you suggested than it shows me an exception `Conflicting changes detected. This may happen when trying to insert multiple entities with the same key.` Sorry for not mentioning this in my question, i update my question. – Gaurav Mar 06 '13 at 18:25
  • Other people have run into this as well with TPT inheritance. Check out http://stackoverflow.com/questions/9064273/cascade-delete-in-entity-framework-table-per-type-inheritance, sounds like there's a bug/feature that makes deleting a bit hairy, but it's possible. Also, each of the relationships that should cascade should be configured like the example I posted above, just to be sure that they cascade (as much as is possible, with the bug/feature explained in that link) – Stephen Fischer Mar 06 '13 at 18:33
  • yaa i already check this post but in my case somehow record is not deleting even from PersonalDetail table (derived table) – Gaurav Mar 06 '13 at 18:35