What is meant by 'The specified LINQ expression contains references to queries that are associated with different contexts'

asp.net-mvc entity-framework linq

Question

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:

1
1
8/9/2013 9:48:27 AM

Accepted Answer

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)

8
8/9/2013 8:48:11 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow