完整的錯誤: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