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 引用实体
基础教程推荐
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 将数据集转换为列表 2022-01-01
- 如果条件可以为空 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
