Please take a look at following code:
public class SomeEntity
{
public int Id { get; set; }
public int UserId { get; set; }
public virtual User User { get; set; }
}
public class SomeEntityA : SomeEntity
{
public int Number { get; set; }
}
public class SomeEntityB : SomeEntity
{
public string Text { get; set; }
}
public class User
{
public int Id { get; set; }
public int Username { get; set; }
public virtual ICollection<SomeEntityA> SomeEntitiesA { get; set; }
public virtual ICollection<SomeEntityB> SomeEntitiesB { get; set; }
}
My question is – is there a way to set the FluentApi to make relationships shown above work properly? Currently when new SomeEntityA object is being added to User, EF creates a new record in SomeEntities table with properly set User_Id FK, however in SomeEntitesA which is an inherited table, there’s also a FK property User_Id – set as null and when I try to get SomeEntitesA collection from User object – it’s empty. I do realize why that happens, but I’m not sure whether there’s a way to fix this? The only solution that comes to my mind at this moment is to replace following code:
public virtual ICollection<SomeEntityA> SomeEntitiesA { get; set; }
public virtual ICollection<SomeEntityB> SomeEntitiesB { get; set; }
with:
public virtual ICollection<SomeEntity> SomeEntitiesA { get; set; }
public virtual ICollection<SomeEntity> SomeEntitiesB { get; set; }
and configure FluentApi.
Any thoughts would be highly appreciated.
Have a look at this question/answer, the classes have exactly the same structure as yours and there is a detailed explanation why things are not working as expected:
Is inheritance of navigation properties supported?
As Slauma pointed out, there is a rather simple solution to get around this by doing the following (copied from the linked answer and fit to your example):