EFCore 将 2 个实体映射到同一个表 [英] EFCore Map 2 entities to same table

查看:36
本文介绍了EFCore 将 2 个实体映射到同一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 DDD 与 EFCore 一起使用,并且正在努力寻找一种方法来将来自不同上下文的 2 个 POCO 映射到同一个表.

I'm trying to use DDD with EFCore and I am struggling to find a way to map 2 POCOs from different context that represent the same entity to the same table.

我在 UserContext 中有一个 User 类,其中包含为我的应用程序创建新用户所需的所有属性.我的 OrderContext 中有一个 User 类,在这个类中我只有 Id 和 Email 属性,因为它是 OrderContext 工作所需的全部.

I have a User class inside UserContext, with all the properties needed to create a new user to my application. And I have either a User class inside my OrderContext, in this class I only have the Id and Email properties, cause it's all that is needed in OrderContext to work.

所以我有这样的事情:

        modelBuilder.Entity<Domain.UserContext.User>(u =>
        {
            u.ToTable("User").HasKey(e => e.Id);
            u.OwnsOne(e => e.Name);
            u.OwnsOne(b => b.HomeAddress);
        });

        modelBuilder.Entity<Domain.OrderContext.User>(u =>
        {
            u.ToTable("User").HasKey(e => e.Id);
        });

        modelBuilder.Entity<Domain.OrderContext.Order>(p =>
        {
            p.ToTable("Order").HasKey(b => b.Id);
            p.HasOne(x => x.User); // this is OrderContext.User
        });

我似乎找不到将两个 User 类映射到同一个表的方法.有办法吗?

I can't seem to find a way to map both User classes to the same table. Is there a way to do it?

Edit1:两个上下文都是有界上下文 DDD 的概念,而不是 DbContext.我只需要将两个类都映射为同一个表.Add-Migration 命令返回一条消息,告诉我它无法将OrderContext.User"映射到表User",因为它已经映射到UserContext.User".

Both contexts are bounded context DDD's concept not DbContext. I just need both classes to be maped as the same table. The Add-Migration command return a message telling me that it cannot map 'OrderContext.User' to table 'User' since it is already mapped to 'UserContext.User'.

推荐答案

问题的主要原因是,EF Core 无法弄清楚如何为 2 个不同的实体使用同一个表.映射中缺少数据,一旦填写,它就会按预期工作.

The main cause of issue is, EF Core cannot figure out how to use same table for 2 different entities. There is lack of data in mapping, once you fill that in, it works as expected.

首先,您需要定义它们之间的关系.使用相同的 PK 共享同一个表不会在服务器端定义外键,但两个实体之间仍然存在内在关系,它们是一对一的并且使用 PK 作为 FK.定义关系后,您将看到它起作用并且两个实体都映射到同一个表.(就像拥有的实体如何映射到同一个表一样).不过,这可能不是您案例的映射结束.由于从 EF 的角度来看,它们仍然是 2 个不同的实体,除了 Id(或 PK 属性),它们将有自己的列来存储数据.但是,如果您有在两个上下文中都通用的列(例如您的场景中的 Email),该怎么办?在这种情况下,您还需要为这些附加列提供映射.如果您将它们显式地映射到同一列,它们将开始在数据库中共享该列.总的来说,代码看起来像这样.

First you will need to define how are they related to each other. Sharing same table with same PK does not have Foreign Key defined on server side but there is still intrinsic relationship between both entities which is one-to-one and using PK as FK. Once you define relationship, you will see that it works and both entities are mapped to same table. (Just like how owned entities are mapped to same table). This may not be end of mapping for your case though. Since from EF perspective they are still 2 different entities, except for Id (or PK property), they will have own columns to store data. But what if you have columns which are common in both the context (like Email in your scenario). In such case, you would need to provide mapping for those additional column too. If you map them to same column explicitly, they will start sharing the column in database. Overall the code would look like this.

namespace UserContext
{
    public class User
    {
        public int Id { get; set; }
        public string Email { get; set; }
        // Other properties
    }
}

namespace OrderContext
{
    public class User
    {
        public int Id { get; set; }
        public string Email { get; set; }
    }
}

// In OnModelCreating method
modelBuilder.Entity<UserContext.User>(u =>
{
    u.ToTable("User");
    u.Property(e => e.Email).HasColumnName("Email");
    // Configuration for other properties
});

modelBuilder.Entity<OrderContext.User>(u =>
{
    u.ToTable("User");
    u.Property(e => e.Email).HasColumnName("Email");
    u.HasOne<UserContext.User>().WithOne().HasForeignKey<OrderContext.User>(e => e.Id);
});

上面的代码创建了一个带有共享列的表,应该可以正常工作.如果需要,您可以按照相同的配置在同一个表中添加更多实体.在这里,我使用 UserContext 中的 User 作为主体端,但您可以使用任何端.我的主要原因是,UserContext.User 将是添加新用户时添加的实体.共享表的实体也不必是子集.但是会有一些列用于未共享的其他属性.

Above code creates single table with shared columns and should work as expected. You can add more entities in the same table if you want by following same configuration. Here, I used User from UserContext as principal side but you can use any side. The main reasoning for me was, UserContext.User will be the entity which will be added when adding new User. Entities sharing the table do not have to be subset either. But there will be columns for additional properties which are not shared.

这篇关于EFCore 将 2 个实体映射到同一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆