0

为什么这 #$*% 这么难?不应该。

两个表 Orders & Shippers:Orders.ship_via,一个 int 是 Shippers.ShipperId Shippers 的 FK。ShipperId 是 Shippers 上的 PK

我的实体:

public class Order : Entity
{
  public int OrderId { get; set; }
  public int ShipVia { get; set; }
  //More properties
  public virtual Shipper Shipper { get; set; }
}

public class Shipper : Entity
{
  public int ShipperId { get; set; }
  //More properties
}

当我运行它时,EF 尝试使用不存在的 Order.ShipperId 自然填充 Order.Shipper。

我不想使用注释。如何创建流畅的地图?

(注意:如果你想运行测试,我的测试环境使用 Northwind)。

4

1 回答 1

2

尝试这样的事情:

modelBuilder.Entity<Order>()
   .HasRequired(o => o.Shipper) // Or .HasOptional if it's not required
   .WithMany() // No reverse navigation property
   .HasForeignKey(o => o.ShipVia)
   .WillCascadeOnDelete(true);
于 2012-12-05T12:58:48.207 回答