我有两个模型:
public class Person
{
public virtual int Id { get; set; }
public virtual Employee Employee { get; set; } // optional
}
public class Employee
{
public virtual int Id { get; set; }
public virtual int PersonId { get; set; }
public virtual Person Person {get; set; } // required
}
public class EmployeeConfiguration : EntityTypeConfiguration<Employee>
{
public EmployeeConfiguration()
{
Property(e=>e.PersonId) // I need this property mapped
.HasColumnName("person_id")
.HasColumnType("int");
}
}
我想使用流利的映射来映射它们。Employee 表的“person_id”列不可为空。我试过以下:
HasRequired(e => e.Person)
.WithOptional(p => p.Employee)
.Map(m => m.MapKey("person_id"));
但它失败了:
System.Data.Entity.ModelConfiguration.ModelValidationException :在模型生成期间检测到一个或多个验证错误:
person_id:名称:类型中的每个属性名称必须是唯一的。属性名称“person_id”已定义。
我自己需要 PersonId 属性,所以我基本上想要的是:
HasRequired(e => e.Person)
.WithOptional(p => p.Employee)
.HasForeignKey(e => e.PersonId); // there is no such method
但是这里没有HasForeignKey这样的方法