2013-10-18 44 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%确定为什么这是必需的,我认为它迫使实体框架使用我的列名称,而不是尝试自己创建它们。如果其他人有更多的投入,请分享!