【发布时间】:2015-06-24 20:55:05
【问题描述】:
完全错误:
在模型生成过程中检测到一个或多个验证错误:
EFEmployee_Identity_Source::多重性在关系“EFEmployee_Identity”中的角色“EFEmployee_Identity_Source”中无效。因为从属角色属性不是关键属性,所以从属角色的多重性的上限必须是'*'。
我正在处理三种类型的实体: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 是依赖方,因为可以有一个不是员工的人,但不能有一个不是人的员工。由于 EFEmployee 是依赖方,我在 EFEmployee 中添加了一个 PersonID,上面的数据注释(属性?)表示它是 Person 的外键:
[ForeignKey("Identity")]
public Guid PersonID { get; set; }
我想我已经对 Entity Framework 说得很清楚,这是一个 1:0..1 的关系。有谁知道如何解决这个错误使用数据注释(或属性,无论属性上面的方括号是什么)。由于我不了解的原因,我无法使用 fluent API。
【问题讨论】:
标签: c# entity-framework foreign-keys data-annotations