2015-06-24 54 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