EF Core 2.2,拥有的实体在层次结构中有多个时生成为另一个 table

EF Core 2.2, owned entities generated as another table when multiple in hierarchy

我有一个模型,其中 class Address 标记为 [Owned] 和人员层次结构(人员、客户或员工,然后是更多子类型等)。此层次结构的不同阶段有地址,所有地址最终都在一个 table 中,因为 EF Core 限制为每个层次结构 table。我希望 address 的所有属性在那个人 table 中出现多次(在任何子类型中每次提及一次)但是它根本没有出现!相反,我看到他们每个人的 FK 和一个单独的地址 table。

EF Core 不支持同一类型的多个拥有成员吗?如果没有,我应该做什么?我没有任何可能会干扰默认设置的流畅 API / 特定配置(新的空控制台项目,只有配置行是 .UseSQLServer(connectionstring)

下面的示例代码:

public class SampleContext : DbContext
{
    public virtual DbSet<Address> Addresses { get; set; }
    public virtual DbSet<Customer> Customers { get; set; }
    public virtual DbSet<Employee> Employees { get; set; }
    public virtual DbSet<Person> Persons { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer("my connection string here");
        }
        base.OnConfiguring(optionsBuilder);
    }
}
[Owned]
public class Address
{
    public int Id { get; set; }
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string AddressLine3 { get; set; }
    public string City { get; set; }
}

public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DateOfBirth { get; set; }
}

public class Employee : Person
{
    public Address Address { get; set; }
}

public class Customer : Person
{
    public Address DeliveryAddress { get; set; }
    public Address InvoicingAddress { get; set; }
}

预期 Person table :

DeliveryAddressAddressLine1
DeliveryAddressAddressLine2
DeliveryAddressAddressLine3
DeliveryAddressAddressCity
InvoicingAddressAddressLine1
InvoicingAddressAddressLine2
InvoicingAddressAddressLine3
InvoicingAddressAddressCity
EmployeeAddressAddressLine1
EmployeeAddressAddressLine2
EmployeeAddressAddressLine3
EmployeeAddressAddressCity

生成了Persontable(+一个意外的Addresstable):

EmployeeAddressAddressId
DeliveryAddressAddressId
InvoicingAddressAddressId

编辑:更新了问题,添加了上下文定义并注意到我有 Addresses 作为 DbSet 所以我认为这可能是原因,删除它会给我以下错误:

Cannot use table 'Person' for entity type 'Customer.DeliveryAddress#Address' since it is being used for entity type 'Employee.Address#Address' and there is no relationship between their primary keys.`

根据 EF Core Owned Entity Types 文档:

Inheritance hierarchies that include owned entity types are not supported

您可以通过将 public Address Address { get; set; }public Address DeliveryAddress { get; set; }public Address InvoicingAddress { get; set; } 导航属性从 EmployeeCustomer 移动到基础 class 来解决这个问题] Person如下:

public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DateOfBirth { get; set; }

    public Address Address { get; set; }
    public Address DeliveryAddress { get; set; }
    public Address InvoicingAddress { get; set; }
}

然后使用 fluent API 配置以覆盖拥有的实体列名称的 Navigation_OwnedEntityProperty 规则,如下所示:

modelBuilder.Entity<Person>().OwnsOne(p => p.Address,
    a =>
    {
         a.Property(p => p.AddressLine1).HasColumnName("EmployeeAddressLine1");
         a.Property(p => p.AddressLine2).HasColumnName("EmployeeAddressLine2");
         a.Property(p => p.AddressLine2).HasColumnName("EmployeeAddressLine3");
         a.Property(p => p.City).HasColumnName("EmployeeAddressCity");
    }).OwnsOne(p => p.DeliveryAddress,
    a =>
    {
        a.Property(p => p.AddressLine1).HasColumnName("DeliveryAddressLine1");
        a.Property(p => p.AddressLine2).HasColumnName("DeliveryAddressLine2");
        a.Property(p => p.AddressLine2).HasColumnName("DeliveryAddressLine3");
        a.Property(p => p.City).HasColumnName("DeliveryAddressCity");
   }).OwnsOne(p => p.InvoicingAddress,
   a =>
   {
        a.Property(p => p.AddressLine1).HasColumnName("InvoicingAddressLine1");
        a.Property(p => p.AddressLine2).HasColumnName("InvoicingAddressLine2");
        a.Property(p => p.AddressLine2).HasColumnName("InvoicingAddressLine3");
        a.Property(p => p.City).HasColumnName("InvoicingAddressCity");
   });

现在,如果您不想将 public Address Address { get; set; }public Address DeliveryAddress { get; set; }public Address InvoicingAddress { get; set; } 导航属性从 EmployeeCustomer 移动到基础 class Person 然后你必须从每个地址类型创建单独的表,如下所示:

modelBuilder.Entity<Employee>().OwnsOne(p => p.Address,
    a =>
    {
        a.ToTable("EmployeeAddresses");
    });

modelBuilder.Entity<Customer>().OwnsOne(p => p.DeliveryAddress,
    a =>
    {
        a.ToTable("DeliveryAddresses");
    }).OwnsOne(p => p.InvoicingAddress,
    a =>
    {
        a.ToTable("InvoicingAddresses");
    });