我有一类Address
标志着[Owned]
和人民层次的模型(人,客户或员工,则更加亚型等等)。还有在这个层次的不同阶段地址和EF核心是限制在每一个分层表中的一个表中的所有的它结束了。我预期所有的地址属性可能会多次出现在人表(每一次提到在任何亚型),但它不会出现在所有!相反,我看到FK为他们每个人的和一个单独的地址表。
难道EF核心不支持多个相同类型的国有成员?如果不是有什么我应该怎么办?我没有,可以用默认值(新的空控制台项目任何干扰流利的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
表:
DeliveryAddressAddressLine1
DeliveryAddressAddressLine2
DeliveryAddressAddressLine3
DeliveryAddressAddressCity
InvoicingAddressAddressLine1
InvoicingAddressAddressLine2
InvoicingAddressAddressLine3
InvoicingAddressAddressCity
EmployeeAddressAddressLine1
EmployeeAddressAddressLine2
EmployeeAddressAddressLine3
EmployeeAddressAddressCity
生成Person
表(+意想不到Address
表):
EmployeeAddressAddressId
DeliveryAddressAddressId
InvoicingAddressAddressId
编辑:更新的问题,增加了上下文定义,发现我有Addresses
作为DbSet,所以我想这可能是原因,删除它给了我下面的错误:
因为它是被用于实体类型“Employee.Address#地址”不能用表“人”为实体类型“Customer.DeliveryAddress#地址”,并有他们的主要keys.`之间没有任何关系
据EF核心Owned Entity Types文档:
包括国有实体类型继承层次结构不支持
您可以克服移动qazxsw POI,从public Address Address { get; set; }
和public Address DeliveryAddress { get; set; }
基类public Address InvoicingAddress { get; set; }
如下qazxsw POI和POI qazxsw导航属性这个问题:
Employee
然后用流利的API配置来覆盖所有的实体列名Customer
规则如下:
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; }
}
和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; }
导航属性,那么你必须创建从每个地址类型不同的表如下:
Employee