gpt4 book ai didi

asp.net-core - EF Core 2.2,当层次结构中有多个时,拥有的实体生成为另一个表

转载 作者:行者123 更新时间:2023-12-03 03:25:49 25 4
gpt4 key购买 nike

我有一个模型,其类Address 标记为[Owned] 和人员层次结构(人员、客户或员工,然后是更多子类型等)。该层次结构的不同阶段都有地址,并且所有地址最终都在一个表中,因为 EF Core 仅限于每个层次结构的表。我预计地址中的所有属性都会在该人员表中多次出现(在任何子类型中每次提及一次),但它根本没有出现!相反,我看到每个地址都有 FK 以及一个单独的地址表。

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; }
}

预期人员表:

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

生成的Person表(+意外的Address表):

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; 来解决这个问题。放; }, 公共(public)地址 DeliveryAddress { get;放; }公共(public)地址 InvoicingAddress { get;放; }EmployeeCustomer 到基类 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 地址 { get;放; }, 公共(public)地址 DeliveryAddress { get;放; }公共(public)地址 InvoicingAddress { get;放; }EmployeeCustomer 到基类 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");
});

关于asp.net-core - EF Core 2.2,当层次结构中有多个时,拥有的实体生成为另一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54509487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com