2014-04-29 34 views
1

我正在玩這個問題幾天,找不到任何解決方案。EF 6 - 如何解決組合鍵的外鍵錯誤?

我使用的代碼第一戰略,.NET MVC 4.5和EF 6

我有兩個型號複合主鍵:

public class Category : DbContext 
{ 
    [Key, Column(Order = 0),DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int CategoryId { get; set; } 

    [Key, Column(Order = 1)] 
    public int ShopId { get; set; } 

    public string Name { get; set; } 
} 

public class Product : DbContext 
{ 
    [Key, Column(Order = 0),DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int ProductId { get; set; } 

    [Key, Column(Order = 1)] 
    public int ShopId { get; set; } 

    public int CategoryId { get; set; } 

    [ForeignKey("CategoryId, ShopId")]   
    public virtual Category Category { get; set; } 
} 

,當我要運行添加遷移命令我得到這個代碼在遷移文件夾:

CreateTable(
    "dbo.Categories", 
    c => new 
    { 
    CategoryId = c.Int(nullable: false, identity: true), 
    ShopId = c.Int(nullable: false), 
    Name = c.String(nullable: false, maxLength: 5)     
    }) 
    .PrimaryKey(t => new { t.CategoryId, t.ShopId }); 

    CreateTable(
    "dbo.Products", 
     c => new 
     { 
     ProductId = c.Int(nullable: false, identity: true), 
     ShopId = c.Int(nullable: false), 
     CategoryId = c.Int(nullable: false) 
     }) 
     PrimaryKey(t => new { t.ProductId, t.ShopId }) 
     .ForeignKey("dbo.Categories", t => new { t.CategoryId, t.ShopId }, cascadeDelete: true)    
     .Index(t => new { t.ShopId, t.CategoryId }); 

,當我運行update-database命令一切正常,直到第一accees通過EF數據庫。我會得到這個錯誤:

Foreign key constraint 'Product_Category' from table Product (ShopId, CategoryId) to table Category (CategoryId, ShopId):: Insufficient mapping: Foreign key must be mapped to some AssociationSet or EntitySets participating in a foreign key association on the conceptual side.

我現在:

  1. 如果我刪除Model類外鍵,EF仍生成外鍵遷移代碼。問題依然存在。

  2. 如果我從生成的代碼中刪除外鍵以進行遷移。問題依然存在。 (即使在DB中沒有任何表之間的外鍵)

  3. 如果我將Product類屬性CategoryId從int更改爲字符串,EF將生成新的兩列到表Product_CategoryId和Product_ShopId,並將外鍵放在這些列上。問題解決了....

真的不明白哪裏的問題。當然,我不想把這兩列放在桌子上。我想從CategoryId和ShopId列中直接獲得外鍵。

真的thx。任何建議。

回答

0

從您的代碼,有什麼具體的原因,爲什麼產品或類別應該從DbContext繼承?另外,嘗試下面,它會工作。我很懷疑它爲什麼失敗的原因是「複合主鍵(在產品)的一部分也爲外鍵(複合FK)從範疇的一部分」

public class SampleContext : DbContext 
    { 
     public IDbSet<Category> Categories { get; set; } 
     public IDbSet<Product> Products { get; set; } 
     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      base.OnModelCreating(modelBuilder); 
     } 
    } 
    public class Category 
    { 
     [Key, Column(Order = 0), DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
     public int CategoryId { get; set; } 
     [Key, Column(Order = 1)] 
     public int ShopId { get; set; } 
     public string Name { get; set; } 
    } 
    public class Product 
    { 
     [Key, Column(Order = 0), DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
     public int ProductId { get; set; } 
     public int ShopId { get; set; } 
     public int CategoryId { get; set; } 
     [ForeignKey("CategoryId, ShopId")] 
     public virtual Category Category { get; set; } 
    } 

讓我知道,如果你是這個什麼期待。並且首選的地方是EF遷移亞軍應該拋出異常?

+0

是的,就是這樣!對你的問題。在模型類中繼承DbContext是錯誤的。 Thx尋求建議。你有權利。主鍵的一部分不能是外鍵的一部分。現在它像我所期望的那樣工作。異常被配置類中的Seed方法拋出。所以遷移中的所有代碼都被執行了,但是當EF第一次連接到數據庫時(無論是哪個表),產品類別中的壞關鍵字例外被拋出。謝謝。再次。你花了我很多時間! – jkubat