Search code examples
asp.netsql-serverentity-framework-4dynamic-datatable-per-type

Need insight on TPT EF4 when Inserting with Dynamic Data


First the error I have:

All objects in the EntitySet 'Z10Entities.Accounts' must have unique primary keys. However, an instance of type 'DAL.Model.CreditCardAccount' and an instance of type 'DAL.Model.Debt' both have the same primary key value, 'EntitySet=Accounts;Id=3'.

Here is my schema: Schema using Table Per Type Inheritance

From my design view point the account can be of both a creditcard type and considered a debt. Have I done something wrong or am I running into a limitation of the EF framework (4.3.1 version)

EDIT: To add some clarity to my question is basically I am trying to understand why the framework is giving me the error or if the dynamic data foreign key FieldTemplate is not working correctly.

EDIT 2: It seems this problem has affected other parts of my application. Seems like I have a TPT problem:

All objects in the EntitySet 'Z10Entities.Person' must have unique primary keys. However, an instance of type 'DAL.Model.Employer' and an instance of type 'DAL.Model.PortalUser' both have the same primary key value, 'EntitySet=Person;Id=1b64b850-34f9-e111-bdc8-000c29005764'.


Solution

  • Well it seems that TPT is great for some things and not for others. Due to my business design I need to have varying accounts be accountable as debt (Accounts charged off, credit cards closed and so on.).

    TPT doesn't handle this because of the PK to PK pattern. It must be FK to PK pointing at the base table. This subtle change facilitates the desired functionality while also simplifying my LinqToEF queries.

    Here is the refactored version: All is correct now :)