该查询包含对在不同数据上下文中定义的项的引用

我在Stackoverflow中阅读了另外几篇文章但我的问题很简单且不同。 我有2个独立的数据库,这就是为什么我有两个单独的Datacontext。 这是我的查询,我传递参数并将其绑定到我的GridView:

if (Session["EntitySelected"] == null) { MessageBox.Show("Please Select an Entity first!"); Response.Redirect("~/FrontEnd/List.aspx"); } int getEntity = Int16.Parse(Session["EntitySelected"].ToString()); this.Label3.Text = "You Selected Entity: " + (string)Session["EntitySelected"]; dbWebEnrollDataContext dt1 = new dbWebEnrollDataContext(); CommissionsV2DataContext cv1 = new CommissionsV2DataContext(); var td = from s in cv1.Entity_Product_Points join r in dt1.PlanMasters on s.Product_ID equals r.Product_ID where s.Entity_ID == getEntity select new { s.Product_ID, r.PlanName, s.HiCommission, s.HiCommissionOld, s.LowCommission, s.LowCommissionOld }; gvShowComm.DataSource = td; gvShowComm.DataBind(); 

正如所料,它给我带来了这个错误。 但是当我在1个数据库的表中添加记录时,我做了同样的事情。 它插入行但也抛出相同的错误。 环顾四周? 谢谢!

我不相信你应该在两个数据上下文中进行连接。 从您的LINQ查询的外观来看,您甚至不使用连接查询中的r来生成任何有意义的内容; 看起来你可以只查询cv1并获取你的数据:

 var td = cv1.Entity_Product_Points.Where(x => x.Entity_ID == getEntity); // Call ToList to fully enumerate the set. 

编辑:查找解决方案,似乎可能就像标记每个数据上下文查询AsQueryable以获得解决方案一样简单 。