EF Code First 1:0..1 relationship error: Multiplicity is not valid in Role 'EFEmployee_Identity_Source' in relationship 'EFEmployee_Identity'
EF Code First 1:0..1 relationship error: Multiplicity is not valid in Role 'EFEmployee_Identity_Source' in relationship '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 实体 class:
[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 实体 class:
[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 是从属方,因为可以有一个人不是雇员,但不能'做不是人的员工。由于 EFEmployee 是从属方,我在 EFEmployee 中添加了一个 PersonID,上面的数据注释(属性?)表示它是 Person 的外键:
[ForeignKey("Identity")]
public Guid PersonID { get; set; }
我想我已经为 Entity Framework 说得很清楚了,这是 1:0..1 关系。有谁知道如何解决此错误 使用数据注释(或属性,无论属性上方的方括号是什么)。我不能流利地使用 API 因为我没有进入的原因。
一般1:0..1关系在Entity Framework中,依赖方需要使用自己的主键作为外键。幸运的是,对于您的情况,这似乎不是一个坏主意。您需要:
- 删除
EFEmployee.PersonID
属性
- 将
[ForeignKey("Id")]
添加到EFEmployee.Identity
编辑:可能不起作用,因为键和导航 属性 位于不同的 类 上。见 this.
EFEmployee
继承自 EFPerson
似乎也是一个可行的选择。默认情况下,继承使用 TPH,但如果您想使用 TPT(table-per-type),请将 [Table]
属性添加到您的类型。
我对模型进行了更多尝试,发现了问题所在。所以我将外键属性保留为 EFPerson.Identity 就像 jjj 建议的那样:
[ForeignKey("PersonID")]
public virtual EFPerson Identity { get; set; }
然后我必须做的另一个改变是在 EFPerson class 中。在我的 EFPerson class 中,我有 属性 到 EFEmployee 的导航:
public virtual EFEmployee Employee { get; set; }
但是,由于这是一个 1:0..1 关系,EFEmployee 是依赖方(即非必要方),我删除了该导航 属性,当我 运行 我的测试有效。
完整错误:
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 实体 class:
[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 实体 class:
[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 是从属方,因为可以有一个人不是雇员,但不能'做不是人的员工。由于 EFEmployee 是从属方,我在 EFEmployee 中添加了一个 PersonID,上面的数据注释(属性?)表示它是 Person 的外键:
[ForeignKey("Identity")]
public Guid PersonID { get; set; }
我想我已经为 Entity Framework 说得很清楚了,这是 1:0..1 关系。有谁知道如何解决此错误 使用数据注释(或属性,无论属性上方的方括号是什么)。我不能流利地使用 API 因为我没有进入的原因。
一般1:0..1关系在Entity Framework中,依赖方需要使用自己的主键作为外键。幸运的是,对于您的情况,这似乎不是一个坏主意。您需要:
- 删除
EFEmployee.PersonID
属性 - 将
[ForeignKey("Id")]
添加到EFEmployee.Identity
编辑:可能不起作用,因为键和导航 属性 位于不同的 类 上。见 this.
EFEmployee
继承自 EFPerson
似乎也是一个可行的选择。默认情况下,继承使用 TPH,但如果您想使用 TPT(table-per-type),请将 [Table]
属性添加到您的类型。
我对模型进行了更多尝试,发现了问题所在。所以我将外键属性保留为 EFPerson.Identity 就像 jjj 建议的那样:
[ForeignKey("PersonID")]
public virtual EFPerson Identity { get; set; }
然后我必须做的另一个改变是在 EFPerson class 中。在我的 EFPerson class 中,我有 属性 到 EFEmployee 的导航:
public virtual EFEmployee Employee { get; set; }
但是,由于这是一个 1:0..1 关系,EFEmployee 是依赖方(即非必要方),我删除了该导航 属性,当我 运行 我的测试有效。