2015-06-24 59 views
0

完整的错误: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,原因是我没有进入。

回答

0

我做了一些更多的模型玩弄,发现什么是错的。所以,我保持着EFPerson.Identity外键的属性一样JJJ建议:

[ForeignKey("PersonID")] 
public virtual EFPerson Identity { get; set; } 

那么其他的变化,我不得不做是在EFPerson类。在我EFPerson类我有导航属性EFEmployee:

public virtual EFEmployee Employee { get; set; } 

然而,由于这是一个1:EFEmployee是因侧(即非基本侧)0..1关系,我删除导航属性,当我跑我的测试它的工作。

+0

仅供参考,我认为这会使关系成为一对多的关系,你也许能够将相同的“EFPerson”分配给多个'EFEmployee''' Identity'。 – jjj

+0

嗯,我想知道是否还有另一个限制,我可以在保持这个模型的同时进行。 – Drew

1

通常,在实体框架中使用1:0..1的关系时,从属方需要使用其主键作为外键。幸运的是,对于你的情况,这似乎并不是一个坏主意。你将需要:

  1. 取出EFEmployee.PersonID财产
  2. 添加[ForeignKey("Id")]EFEmployee.Identity

编辑:可能不行,因为键和导航属性都在单独的类。见this

EFEmployee继承自EFPerson好像它也可能是一个可行的选项。继承默认使用TPH,但如果要使用TPT(按类型),请将[Table]属性添加到您的类型。

+0

嗯,现在我这样做了,我得到了我之前得到的错误:{“无法确定类型'Models.EFPerson'和'Models.EFEmployee'之间的关联的主要结束。此关联必须使用关系fluent API或数据注释来显式配置。“}此外,EFEmployee可以继承EFPerson,但我需要所有实体类从基本实体类EFBusinessEntity继承。 – Drew

+0

啊,射击,我认为你不能做我最初的建议,因为导航属性和外键属性是在不同的类。 (请参阅[this](另一方面,请参阅http://stackoverflow.com/questions/11900155/how-to-map-foreign-keys-between-tph-tpt-objects-entity-framework-code-first)如果'EFEmployee'继承自'EFPerson',并且'EFPerson'继承自'EFBusinessEntity',那么这是不是意味着'EFEmployee'仍然继承自'EFBusinessEntity'? – jjj

+0

谢谢我会看看那个。哦,但是我必须保持这样的实体模型,因为我将它与NoSQL数据库并行创建,并且更容易建立这样的关系。无论如何,我会有更多的实体拥有更多的实体1:0..1的关系和继承不会像一个人和员工那样工作 – Drew