我有一个数据库,我试图在Entity Framework中实现。其中两个表有多对多的关系,似乎实体框架正在尝试创建一个不存在的列名。它坚持认为:
无效的列名称'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和其他论坛,一切似乎都表明我所拥有的是正确的。
答案 0 :(得分:7)
不知怎的,在经过一天努力想要在这里问了10分钟之后,我成功了。
修复方法是将分配配置更改为:
this.HasMany(a => a.Shipments)
.WithMany(x => x.Allocations)
.Map(x =>
{
x.MapLeftKey("AllocationId");
x.MapRightKey("ShipmentId");
x.ToTable("ShipmentAllocation");
});
即添加x =&gt; x.Anocations to WithMany()。
我不是百分之百确定为什么需要它,我认为它迫使Entity Framework使用我的列名而不是尝试自己创建它们。如果有其他人有更多的意见请分享!