查询包含对在不同数据上下文中定义的项目的引用
问题描述:
Func<PageDBDataContext,int,int,string, IQueryable<SepiaWEB.Models.Pages.Page>> s_compiledQuery2 = CompiledQuery.Compile<PageDBDataContext, int,int,string, IQueryable<SepiaWEB.Models.Pages.Page>>(
(ctx, OrganizationId, pagesids,filte) => from pag in pagerepository.GetAllPages()
join pgmt in pagerepository.GetAllPageMeta()
on pag.int_PageId equals pgmt.int_PageId
where (pag.int_OrganizationId == OrganizationId && pag.int_PageId == pagesids
&& pag.int_PostStatusId == 2) &&
(pgmt.vcr_MetaKey.ToLower().Contains(filte) && pgmt.vcr_MetaValue.Contains("true"))
select pag);
using (PageDBDataContext context = new PageDBDataContext())
{
IQueryable<SepiaWEB.Models.Pages.Page> orders = s_compiledQuery2.Invoke(context, 3,1137,"chken");
}
如何删除此错误?查询包含对在不同数据上下文中定义的项目的引用
答
问题是,您正在使用pagerepository.GetAllPages(),并确定它使用的是与ctx不同的datacontext。事实上,它似乎并不像查询使用作为参数传递的datacontext。作为解决办法,我建议从查询中删除页面库引用,并将其替换为ctx的使用,如下所示:
Func<PageDBDataContext,int,int,string, IQueryable<SepiaWEB.Models.Pages.Page>> s_compiledQuery2 = CompiledQuery.Compile<PageDBDataContext, int,int,string, IQueryable<SepiaWEB.Models.Pages.Page>>(
(ctx, OrganizationId, pagesids,filte) => from pag in ctx.Pages
join pgmt in ctx.PagesMeta
on pag.int_PageId equals pgmt.int_PageId
where (pag.int_OrganizationId == OrganizationId && pag.int_PageId == pagesids
&& pag.int_PostStatusId == 2) &&
(pgmt.vcr_MetaKey.ToLower().Contains(filte) && pgmt.vcr_MetaValue.Contains("true"))
select pag);