.netentity-frameworkinheritancesavechangestable-per-type

Unable to save Entity Framework Inherited types


I have implemented some table-per-type inheritance in my data model (basically have a BaseEntity type with all the base information for my items and a Employer type that inherits from the BaseEntity item). Everything appears to be set up correctly and when using the Entities (either via ADO.net Data Services or via Linq to Entities) I can see the Employer type and things appear to be fine. The issue starts when I create a new Employer entity and attempt to save it.

On the context that doesn't appear to be an .AddToEmployer item (only and AddObject or AddToBaseEntity).

If I use AddObject("Employer", NewEmployer) I get and error message of:

The EntitySet name 'DataEntities.Employer' could not be found.

If I use AddToBaseEntity(NewEmployer) I get an error message of:

Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements orstore generated values.

Have I missed a step in setting up the inheritance? Is there some specific way to save objects that are inherited? What am I doing wrong? I assume that the basic issue is that I should have an AddToEmployer, what do I need to do to get that exposed? It seems odd that it is not an option since I can see the Employer type on the client side and can do things such as:

var NewEmployer = new Employer() - which seems to suggest that I can see the Employer type fine.


Solution

  • I changed a couple of things and was able to get this to work. I am not particularly sure what was the base issue, but wanted to post what I did do for reference.

    Rebuilt Tables: I rebuilt the tables starting with just the ID/Key columns and a single data column.

    Removed extra auto incrementing fields: I had an auto-incrementing ID on the BaseEntity and on the Employer. I removed the auto-incrementing ID on the Employer and just had the Employer.BaseEntityID column and the foreign key back to BaseEntity.BaseEntityID. (this seems to have been the culprit, but I was under the impression this was permitted)

    Unfortunately this then lead to the issue that enherited classes in the entity framework cannot have navigation properties (all navigation properties must be on the base entity) so inheritance is going to prove to be non-usable for our needs.