First of all, these questions are similar but definitely not the same:
Can I mix Table per Hierarchy and Table per Type in Entity Framework? - Refers to a different scenario.
Entity Framework: mix table per type and table per hierarchy - Yet another scenario, that despite accepting answer to first scenario is not related to it (*).
(*) Second of all, I have sucessfully mixed table-per-type and table-per-hierarchy in Entity Framework, when base entity is mapped using table-per-entity and the table with the discriminator of the table-per-hierarchy is lower down the chain.
I am trying to map the following:
Tables:
BaseTable
{
int PK1;
int PK2;
string? Value1;
double? Value2;
}
ChildTable3
{
int PK1;
int PK2;
int Value;
}
Entities:
abstract BaseEntity : class // Maps to BaseTable
{
int PK1; // Maps to PK1 in relevant table
int PK2; // Maps to PK2 in relevant table
}
Entity1 : BaseEntity // Maps to BaseTable when Value1 != null
{
string Value; // Maps to Value1
}
Entity2 : BaseEntity // Maps to BaseTable when Value1 == null && Value2 != null
{
double Value; // Maps to value2
}
Entity3 : BaseEntity // Maps to ChildTable3
{
int Value; // Maps to value
}
Prior to adding Entity3 mappings worked.
After adding Entity3 and its mapping, I receive the following error while compiling:
Error 1 Error 3026: Problem in mapping fragments starting at lines 980, 986, 995:Data loss or key constraint violation is possible in table BaseTable. An Entity with Key (PK) will not round-trip when: (PK is in 'BaseTables' EntitySet AND Entity is type [MyNamespace.Entity3]) Path\To\My.edmx 981 15 MyAssembly
I found a workflow that enables performing such mappings via designer:
To add additional child tables later on: