今天在改写架构的时候,遇到这么个错误。当时单从字面意思,看上去错误是由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的更多相关文章

  1. 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 ...

  2. [Linq Expression]练习自己写绑定

    源代码:TypeMapper.zip 背景 项目中,我们会经常用到各种赋值语句,比如把模型的属性赋值给UI,把视图模型的属性拷贝给Entity.如果模型属性太多,赋值也会变成苦力活.所以,框架编程的思 ...

  3. Get Argument Values From Linq Expression

    Get Argument Values From Linq Expression If you even find yourself unpacking an expression in C#, yo ...

  4. C# ORM中Dto Linq Expression 和 数据库Model Linq Expression之间的转换

    今天在百度知道中看到一个问题,研究了一会便回答了: http://zhidao.baidu.com/question/920461189016484459.html 如何使dto linq 表达式转换 ...

  5. C# [LINQ] Linq Expression 获取多格式文件

    using System; using System.IO; using System.Linq; using System.Linq.Expressions; internal static str ...

  6. 查看LINQ Expression編譯後的SQL語法(转)

    在用了LINQ語法之後的一個月,我幾乎把SQL語法全部拋到腦後了,不過 LINQ好用歸好用,但是實際上操作資料庫的還是SQL語法,如果不知道LINQ語法 編譯過後產生怎樣的SQL語法,一不小心效能就會 ...

  7. Expression Tree Basics 表达式树原理

    variable point to code variable expression tree data structure lamda expression anonymous function 原 ...

  8. C#中的LINQ

    从自己的印象笔记里面整理出来,排版欠佳.见谅!   1.LINQ: 语言集成查询(Language Integrated Query) 实例: var q=      from c in catego ...

  9. 转载: 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 ...

随机推荐

  1. iOS开发~UI布局(二)storyboard中autolayout和size class的使用详解

    一.概要:前一篇初步的描述了size class的概念,那么实际中如何使用呢,下面两个问题是我们一定会遇到的: 1.Xcode6中增加了size class,在storyboard中如何使用? 2.a ...

  2. 朝花夕拾-android 一个注册新用户时,多步填写用户资料的框架

    源码地址:http://git.oschina.net/zj2012zy/Android-Demo/tree/master/AndroidDemo/setpregister 效果如下: 基本思路: 1 ...

  3. Effective Java 42 Use varargs judiciously

    Implementation theory The varargs facility works by first creating an array whose size is the number ...

  4. Effective Java 56 Adhere to generally accepted naming conventions

    Typographical naming conventions Identifier Type Type Examples Package com.google.inject, org.joda.t ...

  5. MyCat 学习笔记 第十篇.数据分片 之 ER分片

    1 应用场景 这篇来说下mycat中自带的er关系分片,所谓er关系分片即可以理解为有关联关系表之间数据分片.类似于订单主表与订单详情表间的分片存储规则. 本文所说的er分片分为两种: a. 依据主键 ...

  6. td内元素居顶,td元素不随高度的撑开而变位置

    如下图,右边内容变高了,左边元素位置就下降到居中 设置居顶不变的方法 <table width="200" border="1"> <tr&g ...

  7. 嵌入式Linux应用程序开发详解------(创建守护进程)

    嵌入式Linux应用程序开发详解 华清远见 本文只是阅读文摘. 创建一个守护进程的步骤: 1.创建一个子进程,然后退出父进程: 2.在子进程中使用创建新会话---setsid(): 3.改变当前工作目 ...

  8. Hive beeline update

    Hive cli vs beeline The primary difference between the two involves how the clients connect to Hive. ...

  9. RabbitMQ基本概念和使用

    RabbitMQ是一个消息代理,核心原理:发送消息,接收消息. RabbitMQ主要用于组件之间的解耦,消息发送者无需知道消息使用者的存在,反之亦然.   单向解耦                   ...

  10. JavaScript功能检测技术和函数构造

    Javascript与很多编程语言不同,它不能够控制其运行环境.再写php代码时,只要在服务器端部署了正确的版本,那么程序就绝对能够运行,对于其他python或ruby后端语言来说,也不存在什么灰色区 ...