The specified LINQ expression contains references to queries that are associated with different contexts
今天在改写架构的时候,遇到这么个错误。当时单从字面意思,看上去错误是由join的两个不同的表来源不一致引起的。

其中的videoResult和deerpenList均来自与同一个edmx文件,所以两个表的来源不一致导致了错误的发生,这个猜想是不正确的。
正在左右为难之际,在stackoverflow上面发现了一个主题,正好解决了我的难题。这个问题的回答是这样的:
This can happen if your Context property returns a new instance every time
它的字面意思是:如果你的Context每次访问都返回一个新的实例的话,就会造成这个错误。
回想起我之前的构造:
public interface IContext<T>:IDisposable where T:class
{
DbContext DbContext{get;}
IDbSet DbSet{get;}
} public class Context<T>:IContext<T> where T:class
{
public Context()
{
DbContext = new DbContext(ConfigurationManager.ConnectionStrings["GDeerGardenEntities"].ConnectionString);
DbSet = DbContext.Set<T>();
} public void Dispose()
{
DbContext.Dispose();
} public DbContext DbContext { get; private set; }
public IDbSet<T> DbSet { get; private set; }
}
由于每次调用,都会新建一个DbContext,所以导致错误的发生。找到原因所在,就好了,我们只需要利用autofac这个ioc容器就行,使用的时候,从容器拿就行了。
所以打开安装器,输入
install-package autofac.mvc3 -project GDeerParkWeb
然后安装完毕,注入一下:
builder.RegisterGeneric(typeof(Context<>)).As(typeof(IContext<>)).SingleInstance();
本以为这样就没问题了。但是在使用的时候,依然会出现上述的错误。
到底原因在哪里呢? 这次排查的线索都断掉了。
想了好久,最后发现可能是泛型的Context存在问题,为什么呢?
因为在取实例化的时候,按照目前的设计,实例上下文应该是这样取得:
Context<bas_video>, Context<bas_deerpen>.
这样,带来的问题就显而易见了: 这两个上下文会产生两个不同的实例!!!!!!!
为什么会产生两个不同的实例呢? 因为泛型T只是一个占位符,当实例化出来的时候,泛型的上下文当然会拿不同的实例去hold住,这样就会造成在进行join操作的时候,出现开头的错误。
如果真是这样,那么我们把去掉,不就可以了吗?
这次我们的重构如下:
public interface IContext
{
IDbSet<T> DbSet<T>() where T : class;
DbContext DbContext { get; }
} public class Context:DbContext,IContext
{
public Context()
: base("GDeerGardenEntities")
{} public IDbSet<T> DbSet<T>() where T : class
{
return base.Set<T>();
} public new DbContext DbContext
{
get;
private set;
}
}
我们的容器注入如下:
builder.RegisterType<Context>().As<IContext>().SingleInstance();
最后上阵使用,wow,我们的错误消失了,看来最后的推测是对的。
谨以此文,权当抛砖引玉。
The specified LINQ expression contains references to queries that are associated with different contexts的更多相关文章
- ling join 报错The specified LINQ expression contains references to queries that are associated with different cont
The specified LINQ expression contains references to queries that are associated with different cont ...
- [Linq Expression]练习自己写绑定
源代码:TypeMapper.zip 背景 项目中,我们会经常用到各种赋值语句,比如把模型的属性赋值给UI,把视图模型的属性拷贝给Entity.如果模型属性太多,赋值也会变成苦力活.所以,框架编程的思 ...
- Get Argument Values From Linq Expression
Get Argument Values From Linq Expression If you even find yourself unpacking an expression in C#, yo ...
- C# ORM中Dto Linq Expression 和 数据库Model Linq Expression之间的转换
今天在百度知道中看到一个问题,研究了一会便回答了: http://zhidao.baidu.com/question/920461189016484459.html 如何使dto linq 表达式转换 ...
- C# [LINQ] Linq Expression 获取多格式文件
using System; using System.IO; using System.Linq; using System.Linq.Expressions; internal static str ...
- 查看LINQ Expression編譯後的SQL語法(转)
在用了LINQ語法之後的一個月,我幾乎把SQL語法全部拋到腦後了,不過 LINQ好用歸好用,但是實際上操作資料庫的還是SQL語法,如果不知道LINQ語法 編譯過後產生怎樣的SQL語法,一不小心效能就會 ...
- Expression Tree Basics 表达式树原理
variable point to code variable expression tree data structure lamda expression anonymous function 原 ...
- C#中的LINQ
从自己的印象笔记里面整理出来,排版欠佳.见谅! 1.LINQ: 语言集成查询(Language Integrated Query) 实例: var q= from c in catego ...
- 转载: C#: Left outer joins with LINQ
I always considered Left Outer Join in LINQ to be complex until today when I had to use it in my app ...
随机推荐
- iOS UIButton添加圆角,添加边框
//准备工作 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(,, ...
- yum命令指南-yum使用方法
yum check-update 检查可更新的所有软件包 yum update 下载更新系统已安装的所有软件包 yum upgrade 大规模的版本升级,与yum update不同的 ...
- 手动方式安装 eclipse 的svn插件 Subversive和 Subversive SVN Connectors
0.下载配置jdk 链接:http://pan.baidu.com/s/1miIVuic 密码:mwo7 配置 JAVA_HOME .JRE_HOME 1 下载eclipse ecli ...
- dig与dns基本理论——解析和缓存
DNS(Domain Name System,域名系统)也许是我们在网络中最常用到的服务,它把容易记住的域名,如 www.google.com 翻译成人类不易记住的IP地址,如 173.194.127 ...
- MySQL 强制操作以及order by 使用
我们以MySQL中常用的hint来进行详细的解析,如果你是经常使用Oracle的朋友可能知道,Oracle的hincvt功能种类很多,对于优化sql语句提供了很多方法. 同样,在MySQL里,也有类似 ...
- 手动将自定制的WebPart部署到 SharePoint 2010 中
1.搭建好开发环境,建立webpart工程,写代码. 2.修改assembly.cs文件 在部署前,需要修改assembly文件,增加以下两句: using System.Security; [a ...
- INFORMATICA 的部署实施之 BACKUP&RESTORE
当一套BI 解决方案成熟运行后,公司会快速扩大客户群,这时快速的将开发出来的SOLUTION 应用到全新的生产环境中就很重要了,下面谈谈我做这样项目(INFORMATICA BACKUP&RE ...
- PHP开发第一个扩展
首先声明:我们要构建的是扩展或者模块名为hello_module.该模块提供一个方法:hello_word. 一.PHP环境的搭建 1)一般使用源码包编译安装,而不是binary包安装.因为使用PHP ...
- SQL优化法则小记
SQL优化技巧 1.选择最有效率的表名顺序(只在基于规则的优化器中有效): oracle的解析器按照从右到左的顺序处理 from 子句中的表名,from子句中写在最后的表(基础表 driving ta ...
- 关于点击ztree的节点将页面生成到easyui的新增选项卡(easyui-tabs)时,总是在浏览器中生成一个新的页面的问题
最近的项目中用到了easyui,还有ztree菜单.在这里将我遇到的一些问题写出来算是做个笔记吧. 这是我头一次在博客园里分享代码,我的处女作,写的不好的地方还望各位见谅! 由于很久没有写过前台的东西 ...