Is it possible to model such relation in EF Core?
class EntityA
{
public EntityAId Id {get;set;}
public string Name {get;set;}
}
class EntityB
{
public EntityBId Id {get;set;}
public string Name {get;set;}
}
enum TableTypes {A,B}
class TransitionEntity
{
public int Id {get;set;}
public string TransitionName {get;set;}
public TableTypes TableType {get;set;}
public TableTypeId int {get;set;}
}
EntityAId and EntityBId are strongly typed entity identifiers.
The idea is when table containing TransitionEntity has:
- TransitionEntity.TableType = TableTypes.A then use relation TransitionEntity.TableTypeId == EntityA.Id.Value
- when TransitionEntity.TableType = TableTypes.A then use relation TransitionEntity.TableTypeId == EntityB.Id.Value
I'd like to have one-to-one relation. I tried to use TPH Hierarchy approach but no luck. Is it even possible in EF Core?
For all the others, I figured this out and it is possible. Using TPH is the key.
Configure EntityB in the same way as EntityA. This approach initialy did not work because I had different vales than TableType.A and TableType.B in actual column TableType in TransitionEntity table. So I think it is important to cover all existing values from column which serves as discriminator.
Hope it helps.