完整的错误:EF代码优先1:0..1关系的错误:多重不作用“EFEmployee_Identity_Source”有效的关系“EFEmployee_Identity”
One or more validation errors were detected during model generation:
EFEmployee_Identity_Source: : Multiplicity is not valid in Role 'EFEmployee_Identity_Source' in relationship 'EFEmployee_Identity'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
我处理的三种类型的实体:EFEmployee, EFPerson和EFOffice。我收到这个错误有点奇怪,因为我测试的代码只创建一个EFOffice实体的实例。总之,这里是EFEmployee实体类:
[Table("employee_entity")]
public class EFEmployee : EFBusinessEntity
{
[ForeignKey("Office")]
public Guid OfficeID { get; set; }
[ForeignKey("Identity")]
public Guid PersonID { get; set; }
[Column("hire_date")]
public DateTime HireDate { get; set; }
[Column("job_title")]
public byte[] JobTitle { get; set; }
[Column("salary")]
public int Salary { get; set; }
[Column("certifications")]
public byte[] Certifications { get; set; }
[Column("vacation_time")]
public int VacationTime { get; set; }
[Column("sick_time")]
public int SickTime { get; set; }
public virtual EFOffice Office { get; set; }
public EFPerson Identity { get; set; }
public virtual EFEmployee ReportingTo { get; set; }
}
这是我EFPerson实体类:
[Table("person_entity")]
public class EFPerson : EFBusinessEntity
{
[Column("first_name")]
[StringLength(50)]
public string FirstName { get; set; }
[Column("last_name")]
[StringLength(50)]
public string LastName { get; set; }
[Column("phone_num")]
public uint? PhoneNum { get; set; }
[Column("date_of_birth")]
public DateTime DateOfBirth { get; set; }
public EFEmployee Employee { get; set; }
}
你可以看到,他们都来自EFBusinessEntity,这是在这里继承:
[Table("business_entity")]
public abstract class EFBusinessEntity : IBusinessEntity
{
[Column("tenant_id")]
public Guid TenantId
{
get;
set;
}
[Column("id")]
[Key]
public Guid Id
{
get;
set;
}
}
如您所见,EFEmployee和EFPerson之间存在一对一或一对一的关系,EFEmployee是依赖方,因为可能有一个人不是员工,但是e不能成为不是一个人的雇员。
[ForeignKey("Identity")]
public Guid PersonID { get; set; }
我想我找到了非常清楚的实体:由于EFEmployee在依赖方,我在EFEmployee加了是PersonID与数据注解上述表示,它的外键到人(属性?)这是一个1:0..1关系的框架。有谁知道如何解决这个错误使用数据注释(或属性,无论那些方括号的东西上面的属性)。我无法使用流利的API,原因是我没有进入。
仅供参考,我认为这会使关系成为一对多的关系,你也许能够将相同的“EFPerson”分配给多个'EFEmployee''' Identity'。 – jjj
嗯,我想知道是否还有另一个限制,我可以在保持这个模型的同时进行。 – Drew