簡體   English   中英

EF 6-如何解決復合鍵的外鍵錯誤?

[英]EF 6 - How to solve foreign key error for composite key?

我玩了幾天這個問題,找不到任何解決方案。

我正在使用代碼優先策略,.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; }
}

當我要運行add-migration命令時,我會在遷移文件夾中獲得以下代碼:

 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命令時,一切正常,直到首先通過EF進入數據庫為止。 我會收到此錯誤:

從表Product(ShopId,CategoryId)到表Category(CategoryId,ShopId)的外鍵約束'Product_Category'::映射不足:外鍵必須映射到概念上參與外鍵關聯的某些AssociationSet或EntitySet。

我現在要做什么:

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

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

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

真的不要誤解哪里出了問題。 當然,我不想在表中有這兩列。 我想從CategoryId和ShopId列中獲得直接外鍵。

真的很感謝。 任何建議。

從您的代碼中,是否有任何特定的原因為什么產品或類別應從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 Migration Runner應該拋出異常?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM