Sql 实体框架:;无效的列名";多对多映射错误

Sql 实体框架:;无效的列名";多对多映射错误,sql,entity-framework,Sql,Entity Framework,我有一个数据库,我正试图在实体框架中实现它。其中两个表具有多对多关系,实体框架似乎试图创建一个不存在的列名。它坚持认为存在一个: 列名“装运Id”无效 然而,在我的代码或数据库中没有该列。这两个表是分配表和装运表,按ShipmentAllocation合并 以下是配置: 分配: public AllocationConfiguration() { this.Property(x => x.Id).HasColumnName("AllocationId");

我有一个数据库,我正试图在实体框架中实现它。其中两个表具有多对多关系,实体框架似乎试图创建一个不存在的列名。它坚持认为存在一个:

列名“装运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"));
    }
//
///初始化类的新实例。
/// 
公共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);
这个.有很多(x=>x.Cartons)
.需要(x=>x.装运)
.Map(x=>x.MapKey(“ShipmentId”);
}

我真的不确定到底是怎么回事,我已经搜索了stackoverflow和其他论坛,所有的东西似乎都表明我所做的是正确的。

在这里询问10分钟后,经过一天的努力,我终于找到了答案

修复方法是将分配的配置更改为:

this.HasMany(a => a.Shipments)
    .WithMany(x => x.Allocations)
    .Map(x =>
         {
             x.MapLeftKey("AllocationId");
             x.MapRightKey("ShipmentId");
             x.ToTable("ShipmentAllocation");
          });
即外接程序x=>x.分配给WithMany()的资源

我不能100%确定为什么需要这样做,我认为这迫使实体框架使用我的列名,而不是试图自己创建它们。如果其他人有更多的意见,请分享