什么是“指定的LINQ前pression包含与不同的上下文相关的查询引用'的意思 [英] What is meant by 'The specified LINQ expression contains references to queries that are associated with different contexts'

查看:1627
本文介绍了什么是“指定的LINQ前pression包含与不同的上下文相关的查询引用'的意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Action方法: -

 公众的ActionResult指数(字符串SEARCHTERM = NULL,INT页= 1)
        {
            变种机架= repository.AllFindRacks(SEARCHTERM).OrderBy(一个= GT; a.Technology.Tag).ToPagedList(页,5);            如果(Request.IsAjaxRequest())
            {                返回PartialView(_ RackTable,架);
            }
            返回查看(架);
        }

和下面的方法库: -

 公开的IQueryable< RackJoin> AllFindRacks(串Q)
        {
            清单< RackJoin> rakjoin =新的List< RackJoin>();
            VAR的结果=从tms.TMSRacks架
           .INCLUDE(机架=> rack.DataCenter)
           .INCLUDE(机架=> rack.Zone)
           .INCLUDE(机架=> rack.TMSFirewalls)
           .INCLUDE(机架=> rack.TMsRouters)
           .INCLUDE(机架=> rack.TMSServers)
           .INCLUDE(机架=> rack.TMSStorageDevices)
           .INCLUDE(机架=> rack.TMSSwitches)
           .INCLUDE(机架=> rack.Technology)
                         加入entities.Resources资源
                         .INCLUDE(一个= GT; a.ComponentDefinition)
                           .INCLUDE(一个= GT; a.ResourceLocation.SiteDefinition.SDOrganization)
                           .INCLUDE(一个= GT; a.ResourceLocation.SiteDefinition.AccountDefinition.SDOrganization)
                         在rack.Technology.IT360ID等于resource.RESOURCEID
                         其中,(Q == NULL || rack.Technology.Tag.ToUpper()。StartsWith(q.ToUpper()))
                         选择新RackJoin {
                         机架=架,
                         资源=资源
                         客户= resource.ResourceLocation.SiteDefinition.AccountDefinition.SDOrganization,
                         网站= resource.ResourceLocation.SiteDefinition.AccountDefinition.SDOrganization,
                         技术= rack.Technology                         };
            返回结果;        }

但是,当我打电话给Action方法我收到以下异常: -


  

System.NotSupportedException是由用户code结果未处理
  的HResult = -2146233067消息=指定的LINQ前pression包含
  到与不同的上下文相关联的查询的引用。结果
  来源= System.Data.Entity的堆栈跟踪:



解决方案

看起来你是从两个不同的上下文连接数据:TMS和实体

这是不是在LINQ尽可能都有自己的数据库连接和一个完全独立的模型。

这是不可能的EF转换成SQL语句这一点。 (所有IT人都知道,表可能生活在一个不同的数据库)

您会需要来移动所有实体到一个上下文或单独执行这两个查询,然后在内存中加入他们的行列。 (使用第一个选项,如果所有的表都在同一个数据库,使用第二个,如果你有不同的数据库)

I have the following Action method:-

public ActionResult Index(string searchTerm=null, int page = 1)
        {
            var racks = repository.AllFindRacks(searchTerm).OrderBy(a=>a.Technology.Tag).ToPagedList(page, 5) ;

            if (Request.IsAjaxRequest())
            {

                return PartialView("_RackTable", racks);
            }
            return View(racks);
        }

And the following Repository method:-

public IQueryable<RackJoin> AllFindRacks(string q)
        {
            List<RackJoin> rakjoin = new List<RackJoin>();
            var result = from rack in tms.TMSRacks
           .Include(rack => rack.DataCenter)
           .Include(rack => rack.Zone)
           .Include(rack => rack.TMSFirewalls)
           .Include(rack => rack.TMsRouters)
           .Include(rack => rack.TMSServers)
           .Include(rack => rack.TMSStorageDevices)
           .Include(rack => rack.TMSSwitches)
           .Include(rack => rack.Technology)
                         join resource in entities.Resources
                         .Include(a => a.ComponentDefinition)
                           .Include(a => a.ResourceLocation.SiteDefinition.SDOrganization)
                           .Include(a => a.ResourceLocation.SiteDefinition.AccountDefinition.SDOrganization)
                         on rack.Technology.IT360ID equals resource.RESOURCEID
                         where (q == null || rack.Technology.Tag.ToUpper().StartsWith(q.ToUpper()))
                         select new RackJoin { 
                         Rack = rack,
                         Resource = resource,
                         Customer = resource.ResourceLocation.SiteDefinition.AccountDefinition.SDOrganization,
                         Site = resource.ResourceLocation.SiteDefinition.AccountDefinition.SDOrganization,
                         Technology = rack.Technology

                         };
            return result;

        }

But when i called the Action method i am getting the following exception :-

System.NotSupportedException was unhandled by user code
HResult=-2146233067 Message=The specified LINQ expression contains references to queries that are associated with different contexts.
Source=System.Data.Entity StackTrace:

解决方案

It looks like you are joining data from two different contexts: tms and entities.

This is not possible in LINQ as both have their own connection to the database and a completely separate model.

It's not possible for EF to convert this into a SQL statement. (for all it knows, the tables might live in a different database)

You'd need to either move all your entities to a single context or execute both queries separately and then join them in memory. (use the first option if all tables are in the same DB, use the second if you have separate databases)

这篇关于什么是“指定的LINQ前pression包含与不同的上下文相关的查询引用'的意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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