Colleagues, tell me.
Create a one-or-zero to one-or-zero relationship. There are reference books "users", "racers" and "coaches". Initial conditions: The user may or may not be a racer. It may or may not be a Coach. A driver may or may not be associated with a Portal User. Coach - likewise. Created such models and described the connection. Person base class will be used to store common information for coaches and racers.
public class User { public int Id { get; set; } public string UserName { get; set; } public virtual Rider Rider { get; set; } public virtual Coach Coach { get; set; } } public class Rider : Person { public int Id { get; set; } public string RiderName { get; set; } } public class Coach : Person { public int Id { get; set; } public string CoachName { get; set; } } public class Person { public virtual User User { get; set; } } b.Entity<User>() .HasOptional(u => u.Rider) .WithOptionalDependent(r => r.User) .Map(c => c.MapKey("RiderId")); b.Entity<User>() .HasOptional(u => u.Coach) .WithOptionalDependent(r => r.User) .Map(c => c.MapKey("CoachId")); When creating a migration, I get the error:
User: FromRole: NavigationProperty 'User' is not valid.
Type 'Coach' of FromRole 'User_Coach_Target' in AssociationType 'User_Coach' must be consistent with the type of 'Rider' on which
If you remove the inheritance from the Person class and add the User virtual property to the Coach and Rider classes, then everything is OK, the migration is generated as it should. Tell colleagues, what is the reason for this problem, how to solve it and is it worth it to do it at all?