2012-10-16 173 views
1

情景似乎很微不足道,我真的很困惑我做錯了什麼。實體框架和使用Fluent API將兩個實體映射到另一個

所以,我有一個客戶端類

public class Client 
{ 
    [Key] 
    public int ClientID { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 

    public virtual Account Account { get; set; } 

} 

Employee類

public class Employee 
{ 
    [Key] 
    public int EmployeeID { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 

    public virtual Account Account { get; set; } 
} 

和賬戶類

public class Account 
{ 
    [Key] 
    public int AccountID { get; set; } 
    public string Login { get; set; } 
    public string Password { get; set; } 

    public virtual Employee Employee { get; set; } 
    public virtual Client Client { get; set; } 
} 

客戶和僱員可能有一個帳戶,或不(在線訪問是可選的)。由於數據庫與EF namingconvention不兼容,我必須提供Fluent API顯式映射。

客戶端和員工表都有我試圖用來建立關係的「AccountID」列。

 modelBuilder.Entity<Client>() 
      .HasOptional(e => e.Account) 
      .WithRequired(a => a.Client) 
      .Map(m => m.MapKey("AccountID")); 

     modelBuilder.Entity<Employee>() 
      .HasOptional(e => e.Account) 
      .WithRequired(a => a.Employee) 
      .Map(m => m.MapKey("AccountID")); 

,但我得到

Schema specified is not valid. Errors: 
(15,6) : error 0019: Each property name in a type must be unique. Property name 'AccountID' was already defined. 
(16,6) : error 0019: Each property name in a type must be unique. Property name 'AccountID' was already defined. 

那麼,有沒有辦法比的表/實體結構的改變來解決這個其他?

回答

0

原來你並不需要流利的API在這種情況下,你需要的是與InverseProperty到DataAnnotate在實體的屬性屬性

[InverseProperty("AccountID")] 

有一個在Entity Framework 4.1 InverseProperty Attribute問題有很大答案由拉吉斯拉夫Mrnka

但是,如果有人知道如何做到這一點,流利的答案是高度讚賞

相關問題