I followed this article on making a table-per-type inheritance model for my entities, but I get the following error when I try to add an instance of a subclass to the database.
Here is how I create the subtype:
var cust = db.Users.CreateObject<Customer>(); // Customer inherits User
db.Users.AddObject(cust);
db.SaveChanges();
I get the following error when I make that last call:
"A value shared across entities or associations is generated in more than one location. Check that mapping does not split an EntityKey to multiple store-generated columns."
With the following inner exception:
"An item with the same key has already been added."
Any ideas on what I could be missing?
UPDATED WITH REPRO STEPS
Created a blank MS SQL Server 2008 R2 database, and added the following two tables:
Next I generated the .edmx entity model from the database, and followed the link at the top verbatim. That is to say, I deleted the association between User and Customer, set User as the base class of Customer, deleted the Id property from the Customer entity, and made sure that the Customer.Id column was mapped to the inherited User.Id property. I then ran the following small program:
using (var db = new EF_Test.testEntities())
{
var cust = db.Users.CreateObject<Customer>();
db.Users.AddObject(cust);
db.SaveChanges();
}
I get the same exceptions I described above. Please let me know if this repro works for you, and if it does, what I'm doing wrong. I'm blocked on this issue.
I finally found the source of my troubles. For those still interested, in the Customers table, the Id column should not have been set to the identity column of the table (PK and the FK dependency are fine though).