EF Core 2.0 多重一对一关系

Rob*_*Rob 6 entity-framework-core

我正在尝试创建一个模型,该模型具有一个对Customer实体有两个引用的Address实体:BillingAddressShippingAddress

顾客

public class Customer
{
    public Guid CustomerId {  get;set;}

    public Guid? BillingAddressId { get; set; }
    public Address BillingAddress { get; set; }

    public Guid? ShippingAddressId { get; set; }
    public Address ShippingAddress { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

地址

public class Address
{
    public Guid AddressId { get; set; }

    public Customer Customer { get; set; }
    public Guid CustomerId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

OnModelCreating

modelBuilder.Entity<Address>(eb =>
{
    eb.HasOne(e => e.Customer).WithOne(o => o.BillingAddress).OnDelete(DeleteBehavior.Cascade);
});

modelBuilder.Entity<Address>(eb =>
{
    eb.HasOne(e => e.Customer).WithOne(o => o.ShippingAddress).OnDelete(DeleteBehavior.Cascade);
});
Run Code Online (Sandbox Code Playgroud)

尝试创建迁移时出现以下错误:

Cannot create a relationship between 'Customer.ShippingAddress' and 'Address.Customer', because there already is a relationship between 'Customer.BillingAddress' and 'Address.Customer'. Navigation properties can only participate in a single relationship.

我正在尝试配置模型,以便在删除客户时也删除引用的地址。我希望能够做到这一点,而无需将地址加载到上下文中并依靠数据库进行级联。

viv*_*una 0

我有两点。

  1. 您需要从地址中删除客户参考。
  2. 地址应返回客户的集合。

请参考值对象