I have a hierarchy of classes that is TPH in database. So we have a class Base, and children inheriting from it. Let's say they are ChildrenA, ChildrenB, ChildrenC.
The Base classes hierarchy have a conceptual relation to another classes hierarchy (also expressed in TPH in database) where the base class is RelatedBase, having children class RelatedChildrenA, RelatedChildrenB, RelatedChildrenC.
The conceptual relation is that :
ChildrenAcould be related only to aRelatedChildrenAChildrenBcould be related only to aRelatedChildrenBChildrenCcould be related only to aRelatedChildrenC
I'm using entity framework fluent mapping with independent association MapKey method, to avoid exposing the foreign key id as a property.
So, basically, there is only one foreign key from a TPH hierarchy to another TPH hierarchy.
Given this foreign key is called RelatedId in database, I'm trying to express my fluent mapping as follow :
ModelBuilder.Entity<ChildrenA>().HasOptional(e => e.RelatedChildrenA)
.WithOptionalDependent()
.Map(a => a.MapKey("RelatedId"));
ModelBuilder.Entity<ChildrenB>().HasOptional(e => e.RelatedChildrenB)
.WithOptionalDependent()
.Map(a => a.MapKey("RelatedId"));
ModelBuilder.Entity<ChildrenC>().HasOptional(e => e.RelatedChildrenC)
.WithOptionalDependent()
.Map(a => a.MapKey("RelatedId"));
Unfortunately, this produce the following error :
One or more validation errors were detected during model generation: RelatedId: Name: Each property name in a type must be unique. Property name 'RelatedId' is already defined. RelatedId: Name: Each property name in a type must be unique. Property name 'RelatedId' is already defined.
Is this ever possible to map the same children property to the same base foreign key ?