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

时间:2011-12-13 10:24:06

标签: c# linq

   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");
                }     

如何删除此错误?

1 个答案:

答案 0 :(得分:0)

问题是您使用的是pagerepository.GetAllPages(),并确保它使用的是与ctx不同的datacontext。实际上,查询似乎不使用作为参数传递的datacontext。作为修复,我建议从查询中删除pagerepository引用并将它们替换为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 );