2

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

列名“Shipment_Id”无效。

但是,我的代码或数据库中没有该列。这两个表是 Allocation 和 Shipment,由 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"));
    }

我真的不确定出了什么问题,我已经搜索了 stackoverflow 和其他论坛,一切似乎都表明我所拥有的是正确的。

4

1 回答 1

7

不知何故,在在这里询问 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% 确定为什么需要这样做,我认为它迫使 Entity Framework 使用我的列名,而不是尝试自己创建它们。如果其他人有更多的输入,请分享!

于 2013-10-18T12:20:52.280 回答