Multiple contexts with Entity Framework 6, reference entities across dbcontexts(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 引用实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Entity Framework 6 的多个上下文,跨 dbcontexts 引用实体
基础教程推荐
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
