2013-10-18 46 views
2

我有一個數據庫,我試圖在實體框架中實現。其中兩個表具有多對多的關係,看起來實體框架試圖創建一個不存在的列名。它堅持存在:實體框架:「無效的列名稱」與多對多的映射錯誤

無效的列名'Shipment_Id'。

但是,在我的代碼或數據庫中沒有任何地方存在那個列。這兩個表是分配和發貨,由ShipmentAllocation聯合。

下面是配置: 分配:

public AllocationConfiguration() 
    { 
     this.Property(x => x.Id).HasColumnName("AllocationId"); 
     this.HasKey(x => x.Id); 

     this.Property(x => x.FulfillmentCenter).HasMaxLength(50); 
     this.Property(x => x.DateCreated); 
     this.Property(x => x.DateModified); 
     this.Property(x => x.ModifiedBy).HasMaxLength(50); 

     HasMany(x => x.OrderItems) 
      .WithOptional(x => x.Allocation) 
      .Map(x => x.MapKey("AllocationId")); 

     this.HasMany(a => a.Shipments) 
      .WithMany() 
      .Map(x => 
       { 
        x.MapLeftKey("AllocationId"); 
        x.MapRightKey("ShipmentId"); 
        x.ToTable("ShipmentAllocation"); 
       }); 

    } 

裝運:

/// <summary> 
    /// Initializes a new instance of the <see cref="ShipmentConfiguration"/> class. 
    /// </summary> 
    public ShipmentConfiguration() 
    { 
     this.Property(x => x.Id).HasColumnName("ShipmentId"); 
     this.HasKey(x => x.Id); 

     this.Property(x => x.DateCreated); 
     this.Property(x => x.DateModified); 
     this.Property(x => x.ModifiedBy).HasMaxLength(50); 

     this.HasMany(x => x.Cartons) 
      .WithRequired(x => x.Shipment) 
      .Map(x => x.MapKey("ShipmentId")); 
    } 

我真的不知道什麼是錯的,我已經走遍計算器和其他論壇,一切似乎表明,我所擁有的是正確的。

回答

6

不知何故,我在經過了一天的努力,在詢問這裏10分鐘後才發現它。

此修復程序是分配的配置更改爲:

this.HasMany(a => a.Shipments) 
    .WithMany(x => x.Allocations) 
    .Map(x => 
     { 
      x.MapLeftKey("AllocationId"); 
      x.MapRightKey("ShipmentId"); 
      x.ToTable("ShipmentAllocation"); 
      }); 

即X => x.Allocations添加到WithMany()。

我不是100%確定爲什麼這是必需的,我認爲它迫使實體框架使用我的列名稱,而不是嘗試自己創建它們。如果其他人有更多的投入,請分享!