我的代码如下
public class User
{
public int ID { get; set; }
public int BillingAddressID { get; set; }
public Address BillingAddress { get; set; }
public IList<Shipment> Shipments { get; set; }
}
public class Address
{
public int ID { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string ZipCode { get; set; }
}
public class Shipment
{
public int ID { get; set; }
public string State { get; set; }
public int DeliveryAddressID { get; set; }
public Address DeliveryAddress { get; set; }
public User ShipUser { get; set; }
//[ForeignKey("ShipUser")]
public int ShipUserID { get; set; }
//public int UserId { get; set; }
}
public class TestContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Address> Addresses { get; set; }
public DbSet<Shipment> Shipments { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Shipment>().HasRequired(u => u.ShipUser)
.WithMany(d => d.Shipments)
.HasForeignKey(c => c.ShipUserID)
.WillCascadeOnDelete(false);
}
}
无论如何我试试,但我不知道原因,我需要一些关于EF协会的书。
答案 0 :(得分:0)
如果您没有为一个关系指定没有cascadeDelete = false的映射,那么如果您与user
的{{1}}有两个关系,它将创建多个级联路径。
按照惯例,您可以使用公共
Shipment
它将按惯例使用
Public User ShipUser { get; set; }
作为外键。
public int ShipUserID { get; set; }
如果删除ShipUserID,则需要创建自己的外键以保持关系。这是你的'Shipment_UserID'
ShipUserID
我不明白你的意思。