Entity Framework 6 的多个上下文,跨 dbcontexts 引用实体 [英] Multiple contexts with Entity Framework 6, reference entities across dbcontexts

查看:16
本文介绍了Entity Framework 6 的多个上下文,跨 dbcontexts 引用实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用通用 UnitOfWork 编写两个 MVC5(使用 EF6 和代码优先)Web 应用程序,它获取 Unity 注入的 dbContext.

I am writing two MVC5 (with EF6 and code-first) web apps using generic UnitOfWork that gets the dbContext injected by Unity.

我们需要有两个数据库(主数据库和项目特定数据库)并且在两者之间有一个引用.

We are required to have two databases (Main Database and Project Specific DB) and have a reference between the two.

这是一个例子:

  • 在主要上下文中,我有一个实体 Employee
  • 在项目上下文中,我有一个实体 Department

我需要在我的项目中创建部门,对 Main 中的员工进行分组和组织.

I need to create Departments in my Project that group and organize Employees from Main.

我可以...

ICollection<员工>员工{得到;放;} 在 Project DB 的 Department 实体中?(员工与部门之间是多对多的关系)

ICollection<Employee> Employees { get; set; } in the Project DB's Department Entity ? (the relationship between Employees and Departments is many-to-many)

你会怎么做这样的事情?

How would you go about having something like this done ?

推荐答案

最简单的方法是将表从一个数据库链接到另一个数据库,这样数据库服务器就可以将两个物理数据库作为一个虚拟数据库处理.

The easiest approach would be to link tables from one database to the other so that this is the database server which handles two physical databases as one virtual database.

p>

然后,在 EF 中,您只有一个上下文,其中包含来自两个数据库的表和查询/连接等工作正常.

Then, in EF, you have just one context with tables from both databases and querying/joins etc. works correctly.

http://msdn.microsoft.com/en-us/library/ff772782.aspx

否则,用 EF 和两个上下文链接应用层中的两个数据库听起来是不可能的.

Otherwise, linking two databases in the application layer with EF and two contexts sounds impossible.

这篇关于Entity Framework 6 的多个上下文,跨 dbcontexts 引用实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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