实体框架:跨不同DbContexts共享实体 [英] Entity Framework : Sharing entities across different DbContexts

查看:96
本文介绍了实体框架:跨不同DbContexts共享实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EF6开发一个插件应用程序,首先是代码。



我有一个主要上下文,一个名为用户

  public class MainDataContext:DbContext 
{
public MainDataContext():base MainDataContextCS){}
public DbSet< User>用户{get;组; }
}

然后另一个PluginX的上下文,另一个引用基础的项目:

  public class PluginDataContext:DbContext 
{
public PluginDataContext():base(MainDataContextCS) {
}

protected override void OnModelCreating(DbModelBuilder modelBuilder){
modelBuilder.HasDefaultSchema(PluginX);
base.OnModelCreating(modelBuilder);
}

public DbSet< Booking>预订{get;组; }
}

这个整齐地创建在同一个数据库(同一个连接字符串)上, PluginX.Bookings 表。



这里的问题是预订实体包含对用户实体的引用:

  public课程预订
{
public int Id {get; set;}
public virtual User CreationUser {get; set;}
public BookingStatus Status {get;组;
}

当运行添加迁移用于插件上下文EF将尝试创建另一个用户实体称为 PluginX.User



如何解决?有没有办法在另一个 DbContext 中共享一个共同的实体?

解决方案

p>当您使用多个上下文时,您有两个选项:


  1. 将每个上下文视为单独的应用程序。想像您的用户是从Web服务获得的外部资源。您将无法添加外键。您将做什么是在表中添加只有userId,当您需要用户详细信息调用外部服务以获取它们或在预订上下文中具有用户的本地轻型副本,您将每次更新,然后再更新来自用户上下文。当您使用大型系统并且想要隔离部件(阅读有关DDD和有界环境)时,这种方法是很好的。

  2. 从您的2个上下文中的一个部分,创建第三个上下文整个模型(用户,预订等)。您将使用完整的上下文创建迁移并维护数据库结构,但在应用程序中,您将使用较小的上下文。这是一个非常简单的解决方案。使用单个上下文来维护迁移很容易,并且还允许您在无法访问不相关实体的较小上下文中隔离DB操作。


I'm developing a plugin application with EF6, code first.

I have one main context with an entity called User:

public class MainDataContext : DbContext
{
    public MainDataContext(): base("MainDataContextCS") {}
    public DbSet<User> Users { get; set; }
}

And then another context for PluginX, on another project which references the base one:

public class PluginDataContext : DbContext
{
    public PluginDataContext () : base("MainDataContextCS") {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
        modelBuilder.HasDefaultSchema("PluginX");
        base.OnModelCreating(modelBuilder);
    }

    public DbSet<Booking> Bookings { get; set; }
}

And this neatly creates, on the same Database (same connection string), the PluginX.Bookings Table.

The problem here is that the Booking entity contains a reference to User entity:

public class Booking
{
    public int Id { get; set;}
    public virtual User CreationUser { get; set;}
    public BookingStatus Status { get; set; }
}

And when running Add-Migration for the plugin context EF will try to create another User entity called PluginX.User.

How can this be solved? Is there a way to share a common entity, in another DbContext?

解决方案

When you work with multiple contexts you have two options:

  1. Treat each context like they were separate applications. Imagine your user is an external resource that you get from a web service. You won't be able to add a foreign key to that. What you would do in this is either add only the userId in your tables and when you need the user details call the external service to get them or have a local light copy of the user in the Bookings context that you would update every now and then from the Users context. This approach is good when you work with a large system and you want to isolate the parts (read about DDD and bounded contexts)
  2. A part from your 2 contexts, create a third context with the whole model (users, bookings, etc). You will use the complete context to create the migrations and maintain the DB structure, but in the application you will use the smaller contexts. This is a very simple solution. It's easy to maintain the migrations with a single context and it still allows you to isolate the DB operation in smaller contexts that don't have access to unrelated entities.

这篇关于实体框架:跨不同DbContexts共享实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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