简体   繁体   English

具有使用实体框架核心的集合关系的拥有实体

[英]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.

相关问题 在实体框架核心中使用一对多关系更新连接的实体 - Updating connected entities with one-to-many relationships in entity framework core 如何使用 Entity Framework Core 定义一个实体 class 作为一个集合在多个实体之间共享? - How do I define an entity class to be shared as a collection across multiple entities using Entity Framework Core? 实体框架核心2.2具有UseLazyLoadingProxies的拥有实体 - Entity Framework Core 2.2 Owned Entity with UseLazyLoadingProxies 实体框架核心:将拥有的属性与继承相结合 - Entity Framework Core: Combine owned properties with inheritance 改变与实体框架核心的关系 - change relationships with entity framework core 与Entity Framework Core建立关系 - Create relationships with Entity Framework Core 插入与现有实体有关系的新实体时出现的问题(Entity Framework Core 1.1.0) - Issue when inserting new entity with relationships to existing entities (Entity Framework Core 1.1.0 ) 实体框架核心:使用复合键映射1到1关系 - Entity-Framework-Core: Mapping 1 to 1 Relationships using Composite Keys 在 Entity Framework Core 中,如果我定义这些关系,它会在两个实体之间创建循环关系吗? - In Entity Framework Core, if I define these relationships, will it create a cyclic relationship between two entities? 实体框架核心 - 两个实体之间的多个一对多关系 - Entity Framework Core - Multiple one-to-many relationships between two entities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM