2013-07-15 29 views
5

請檢查可能的解決方案部分下無法確定首席結束 - 實體框架代碼優先關係

我有一個外鍵關係問題 - 這裏是我的表:

public class Lead 
{ 
    [Key] 
    public int LeadId { get; set; } 
    public int? CustomerId { get; set; } 

    [ForeignKey("CustomerId")] 
    public virtual Customer Customer { get; set; } 
} 

public class Customer 
{ 
    [Key] 
    [Column("customer_id")] 
    public int CustomerId { get; set; } 

    [ForeignKey("CustomerId")] 
    public virtual Lead Lead { get; set; } 

} 

我m有一個問題,我收到此錯誤:

Unable to determine the principal end of an association between the types 'Sales.Customers.Customer' and 'Sales.Leads.Lead'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations. 

我已經嘗試將關係添加到模型構建器,但它appe ars說它不能正常工作。當我得到錯誤信息消失時,其實際使用Lead.LeadId -> Customer.CustomerId作爲關係而不是Lead.CustomerId -> Customer.CustomerId的關係。

我檢查了Stackoverflow上的類似問題,但它們似乎與我的數據庫結構不匹配,當我嘗試實施其建議時,關係仍然無法正常工作。

它真的很奇怪 - 非常感謝幫助!

UPDATE

所以我試圖讓這種關係的工作,我周圍的切換鍵以下列方式:

public class Lead 
{ 
    [Key] 
    public int LeadId { get; set; } 

    [ForeignKey("LeadId")] 
    public virtual Customer Customer { get; set; } 
} 

public class Customer 
{ 
    [Key] 
    [Column("customer_id")] 
    public int CustomerId { get; set; } 
    public int? LeadId { get; set; } 

    [ForeignKey("LeadId")] 
    public virtual Lead Lead { get; set; } 
} 

然而,同樣的錯誤,仍然沒有運氣 - 我真的很茫然,爲什麼這種關係不起作用。對我來說,這似乎很簡單。

更新2

確定 - 浪費時間TON與此搞亂後,我已經嘗試了略有不同的方法:

這裏是我的新類....

public class Lead 
    { 
     [Key, ForeignKey("Customer")] 
     public int LeadId { get; set; } 
     public virtual Customer Customer { get; set; } 
    } 

    public class Customer 
    { 
     [Key] 
     [Column("customer_id")] 
     public int CustomerId { get; set; } 
     public int? LeadId { get; set; } 
     public virtual Lead Lead { get; set; } 
    } 

沒有更多的錯誤消息與上面的代碼!唯一的問題是,關係實體框架創建的是Customer.CustomerId和Lead.LeadId之間,而不是Customer.LeadId和Lead.LeadId - 我覺得我很靠近!

可能的解決方案

好了 - 所以經過一番詳細研究,我碰到這個帖子在這裏傳來: EF Code First - 1-to-1 Optional Relationship

我修改了類此:

public class Customer 
{ 
    [Key] 
    [Column("customer_id")] 
    public int CustomerId { get; set; } 

    public virtual Lead Lead { get; set; } 
} 

public class Lead 
{ 
    [Key] 
    public int LeadId { get; set; } 
    public virtual Customer Customer { get; set; } 
} 

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    modelBuilder.Entity<Customer>().HasOptional<Lead>(l => l.Lead).WithOptionalDependent(c => c.Customer).Map(p => p.MapKey("LeadId")); 
    base.OnModelCreating(modelBuilder); 
} 

一切都很正常!但一個大問題...

我不得不從客戶表中刪除LeadId屬性....所以現在我不知道如何在創建新客戶時(如果適用)如何分配LeadId是否分配給LeadId屬性?

+0

只是可以肯定之前,我越挖越深,你打算,這是客戶和領導之間一比一的關係? – Tim

+0

呃...是的 - 這是正確的 - 但是,客戶沒有需要的潛在客戶 - 客戶可以首先進入系統,而不必先作爲潛在客戶 - 知道我的意思嗎?也可以有一個沒有客戶的領導,因爲在一個領導不轉換成客戶 – 99823

+1

我陷入了困境。只是想確保你不打算採用一對多的方式。因爲那是一個不同的野獸。讓我們看看... – Tim

回答

2

發佈這個流利的API,它應該工作。

public class Lead 
{ 
    [Key] 
    public int LeadId { get; set; } 

    public virtual Customer Customer { get; set; } 
} 

public class Customer 
{ 
    [Key] 
    [Column("customer_id")] 
    public int CustomerId { get; set; } 

    public virtual Lead Lead { get; set; } 
} 

builder.Entity<Lead>() 
.HasOptional(l => l.Customer) 
.WithOptionalPrincipal() 
.Map(k => k.MapKey("LeadId")); 

builder.Entity<Customer>() 
.HasOptional(c => c.Lead) 
.WithOptionalPrincipal() 
.Map(k => k.MapKey("CustomerId")); 

編輯

Tables

+0

好吧 - 所以我得到一個新的錯誤:「'字段列表中'未知列'Extent10.LeadId' - 它似乎是創建一個基於Customer Table上的模型構建器代碼的新屬性...試圖選擇當Lead_id是該表中的實際列名稱時,從客戶表中提取LeadId ..其奇怪的.. – 99823

+0

'Extent10'.'customer_id', 'Extent10'.'lead_id', 'Extent10'.company_name', 'Extent10'.'primary_customer_account_id', 'Extent10'.'website_address', 'Extent10'.'Status', 'Extent10'.'investigation_criteria', 'Extent10'.'administrative_criteria', 'Extent10'.' credit_hold', 'Extent10'.'paperwork_on_file', 'Extent10'.'gets_partial', 'Extent10'.'create_datetime', 'Extent10'.'edit_datetime', 'Extent10'.'LeadId' < - 這列不存在 – 99823

+0

其實我認爲我可能已經解決了...無視以上 – 99823