I have two models as below:
public class Person{
public virtual int Id { get; set; }
public virtual int BaseId { get; set; }
public virtual string Name { get; set; }
public virtual Employee Employee { get; set; }
}
public class Employee{
public virtual int Id { get; set; }
public virtual string Code{ get; set; }
public virtual Person Person { get; set; }
}
Every Employee is a Person, but every Person is not necessarily an Employee. Relation between these two is type of One-One relation, but I need to make this relation between a non-primary key column(Person.BaseId) and the desired foreign key column(Employee.Id). In face the Id column in Employee model is the primary key and foreign key column at the same time.
I have this mapping configuration:
public override void Configure(EntityTypeBuilder<Person> builder)
{
builder.HasKey(x => x.Id);
builder.ToTable("tblPeople", "dbo");
builder
.HasOne(p => p.Employee)
.WithOne(p => p.Person)
.HasForeignKey<Employee>(p => p.Id)
.HasPrincipalKey<Person>(p => p.BaseId);
}
public override void Configure(EntityTypeBuilder<Employee> builder)
{
builder.HasKey(x => x.Id);
builder.ToTable("tblEmployees", "dbo");
}
When I try to generate the migration I get the following error:
The child/dependent side could not be determined for the one-to-one relationship between 'Employee.Person' and 'Person.Employee'. To identify the child/dependent side of the relationship, configure the foreign key property. If these navigations should not be part of the same relationship configure them without specifying the inverse. See http://go.microsoft.com/fwlink/?LinkId=724062 for more details.
I do not want to use the Data Annotation approach to solve this problem.
Configuremethod is not called (one of the reasons I don't like separate entity type configuration classes). - Ivan Stoev