Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8366959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:02:41+00:00 2026-06-09T13:02:41+00:00

I’m using EF4.1 with Code first and TPT (Table per Type) inheritance. I have

  • 0

I’m using EF4.1 with Code first and TPT (Table per Type) inheritance. I have a structure like this

public class Customer 
{
    public virtual ICollection<Product> Products {get; set;}
}

public class Product
{
   [Required]
   public int Id { get; set; }

   [Required]
   public virtual Customer {get; set;}

   public decimal Price { get; set; }
}

public class SpecializedProduct : Product
{
   public string SpecialAttribute { get; set; }
}

when i delete a customer i want all the products associated with that customer to be deleted. I can specify a WillCascadeOnDelete(true) between the Customer and the Product:

modelBuilder.Entity<Customer>().HasMany(e => e.Products).WithRequired(p => p.Customer).WillCascadeOnDelete(true);

but since there’s a foreighn key relationship between SpecializedProduct and Product i get an Exception when I try to delete the Customer:

The DELETE statement conflicted with the REFERENCE constraint “SpecializedProduct _TypeConstraint_From_Product_To_SpecializedProduct”. The conflict occurred in database “Test”, table “dbo.SpecializedProduct”, column ‘Id’. The statement has been terminated.

If i manually set a on delete cascade on the SpecializedProduct _TypeConstraint_From_Product_To_SpecializedProduct constraint it works, but i would like to be able to specify this using the modelbuilder or some other way in code. Is this possible?

Thanks in advance!

Best Regards

Simon

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:02:43+00:00Added an answer on June 9, 2026 at 1:02 pm

    When it comes to database, a TPT inheritance is implemented with a Shared Primary Key Association between the base class (e.g. Product) and all the derived classes (e.g. SpecializedProduct). Now, when you delete a Customer object without fetching its Products property, EF has no idea that this Customer has a bunch of products that also needs to be deleted as per your requirement. If you enable cascade deletes by marking your customer-product association as required, then database will take care of deleting the child record(s) from the product table but if this child record is a SpecializedProduct then the related row on the SpecializedProduct won’t get deleted and hence the exception that you are getting. So basically the following code won’t work:

    // This works only if customer's products are not SpecializedProduct
    Customer customer = context.Customers.Single(c => c.CustomerId == 1);
    context.Customers.Remove(customer);
    context.SaveChanges();    
    

    This code will cause EF to submit the following SQL to the database:

    exec sp_executesql N'delete [dbo].[Customer] where ([CustomerId] = @0)',N'@0 int',@0=1
    

    That said, There is no way to enable the cascade deletes between Product and SpecializedProduct tables, that’s just how EF Code First implements a TPT inheritance and you cannot override it.

    So what’s the solution?

    One way is what you already figured out, manually switching the cascades on between Product and SpecializedProduct tables to avoid the exception when you deleting a customer with SpecializedProducts.

    The second way is to let EF take care of the customer’s SpecializedProducts when you removing the customer. Like I said before, this happens because the Customer object has not been properly fetched, and EF has no knowledge of customer’s SpecializedProducts which means by fetching the customer object properly, Ef will start tracking the customer’s associations and will submit necessary SQL statements to make sure that every related record is removed before removing the customer:

    Customer customer = context.Customers
                               .Include(c => c.Products)
                               .Single(c => c.CustomerId == 1);
    
    context.Customers.Remove(customer);
    context.SaveChanges();    
    

    As a result, EF will submit the following SQL statements to the database which perfectly removes everything in order:

    exec sp_executesql N'delete [dbo].[SpecializedProduct] where ([Id] = @0)',N'@0 int',@0=1
    
    exec sp_executesql N'delete [dbo].[Product] where (([Id] = @0) and ([Customer_CustomerId] = @1))',N'@0 int,@1 int',@0=1,@1=1
    
    exec sp_executesql N'delete [dbo].[Customer] where ([CustomerId] = @0)',N'@0 int',@0=1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.