[英]Owned entities with collection relationships using entity framework core
I am trying to model the following class structure where PersonDetails
is an owned entity using table splitting ie there is no table in the db schema called PersonDetails
我正在尝试使用表拆分为以下类结构建模,其中PersonDetails
是拥有实体,即,在数据库架构中没有名为PersonDetails
I am using entity framework core 2.2 我正在使用实体框架核心2.2
public class Person
{
public long Id { get; set; }
public PersonDetails Details { get; set; }
}
public class PersonDetails
{
public IReadOnlyCollection<Address> Addresses { get; set; }
public IReadOnlyCollection<Contact> Contacts { get; set; }
}
public class Address
{
public string Street { get; set; }
public string Suburb { get; set; }
}
public class Contact
{
public string PhoneNumber { get; set; }
public string EmailAddress { get; set; }
}
The entity type maps appear as follows: 实体类型映射如下所示:
public class PersonTypeConfiguration : IEntityTypeConfiguration<Person>
{
public void Configure(EntityTypeBuilder<Person> builder)
{
builder.Property(p => p.Id).ValueGeneratedOnAdd();
builder.OwnsOne(p => p.Details, b =>
{
b.HasMany(p => p.Addresses).WithOne().OnDelete(DeleteBehavior.Cascade);
b.HasMany(p => p.Contacts).WithOne().OnDelete(DeleteBehavior.Cascade);
});
}
}
public class AddressTypeConfiguration : IEntityTypeConfiguration<Address>
{
public void Configure(EntityTypeBuilder<Address> builder)
{
builder.Property<string>("Id").ValueGeneratedOnAdd();
}
}
public class ContactTypeConfiguration : IEntityTypeConfiguration<Contact>
{
public void Configure(EntityTypeBuilder<Contact> builder)
{
builder.Property<int>("Id").ValueGeneratedOnAdd();
}
}
This configuration throws the following error: 此配置引发以下错误:
'The relationship from 'Address' to 'PersonDetails.Addresses' is not supported because the owned entity type 'PersonDetails' cannot be on the principal side of a non-ownership relationship.' 不支持“从'地址'到'PersonDetails.Addresses'的关系,因为拥有实体类型'PersonDetails'不能位于非所有权关系的主体上。”
Is this mapping supported in EF core? EF核心支持此映射吗? I was not able to find this in the docs but perhaps I missed it. 我无法在文档中找到它,但也许我错过了。
If it's not supported does anyone know if there are plans to support it? 如果不支持,是否有人计划支持它?
A full sample can be found here https://github.com/RossJayJones/entity-framework-core-samples 完整的示例可以在这里找到https://github.com/RossJayJones/entity-framework-core-samples
This is not supported by design. 设计不支持此功能。 Owned types are part of an aggregate and only the aggregate root can be referenced from outside. 拥有的类型是聚合的一部分,并且只能从外部引用聚合根。
You can still have the same classes and use table splitting, just don't configure PersonDetails as owned. 您仍然可以拥有相同的类并使用表拆分,只是不要将PersonDetails配置为拥有。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.