【问题标题】:Is it possible to use two context entities from two different database是否可以使用来自两个不同数据库的两个上下文实体
【发布时间】:2017-07-05 07:41:00
【问题描述】:

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

代码连接到一个实体:

 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

【问题讨论】:

    标签: c# linq-to-entities edmx


    【解决方案1】:

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

    1. Database1 中的用户
    2. Database2 中的 Orders,其中数据库 1 中 User 表的 UserId 引用 Orders 表中的 OrderedBy。

    我创建了 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(); 
        } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多