是否可以使用来自两个不同数据库的两个上下文实体



是否可以使用来自两个不同数据库的两个上下文实体

代码连接到一个实体:

Using one FoodSupplyEntities
using (var contextFood = new FoodSupplyEntities())
{
var _result = (from _FoodSupplyStatus in contextFood.FoodSupplyStatus
join _FoodQualityStatus in contextFood.FoodQualityStatus

但是,是否可以从另一个服务器的不同实体中联接另一个表。?

示例(不知道,但它可能会像这样。

using (var contextFood = new FoodSupplyEntities() and contextKitchenware = new KitchenwareEntities() )
{
var _result = (from _FoodSupplyStatus in contextFood.FoodSupplyStatus
join _KitchenwareSupplyStatus in contextKitchenware.KitchenwareSupplyStatus

假设您在 2 个不同的数据库中有 2 个表:

  1. 数据库中的用户1
  2. 数据库
  3. 2 中的订单,其中数据库 1 中用户表的用户 ID 引用订单表中的排序者。

我创建了 2 个不同的上下文。 现在,我将为 2 个不同的上下文创建 2 个查询,并将在 UserId 和 OrderedBy 上对这些查询进行联接,如下所示:

List<OrderDetails> GetOrderDetails()
{
var users = this.TestDb1ModelUnit.GetRepository<User_Login>().GetAll();
var orders = this.TestDb2ModelUnit.GetRepository<OrderDetail>().GetAll();
var orderDetails = users.Join(orders,
usr => usr.User_Id,
ord => ord.OrderedBy,
(usr, ord) => new { usr, ord }
).ToList(); 
} 

最新更新