处置的DataContext导致无效试图调用时,请阅读阅读器关闭 [英] Disposing datacontext causes Invalid attempt to call Read when reader is closed

查看:184
本文介绍了处置的DataContext导致无效试图调用时,请阅读阅读器关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建一个MVC 2应用程序,并使用LINQ to SQL中使用存储的特效。

I'm building an MVC 2 app and using linq to sql with stored procs.

我创建了一个内部​​的DataContext类和我揭露应用的公共类数据访问层。在我的公开I级暴露其访问的DataContext类的方法和使用LINQ将数据转换为自己的对象模型类。

I created a data access layer that has an internal datacontext class and a public class that I expose applications. In my public class I expose methods which access the datacontext class and convert the data to my own object model classes using linq.

在我的公开课,我会用下面的图案暴露的方法:

In my public class, I would expose a method using the following patter:

public IEnumerable<MyObject> ListObjects(int iParameter)
{
    using (MyDataContext db = new MyDataContext)
    {
        //call stored proc and convert results to my object model
        return db.List_Objects().Select(o => new MyObject()
            {
                ID = o.ID,
                Name = o.Name
                Text = o.Code + " " + o.Description
            };
    } 
}

我的MVC应用程序将调用从模型类此方法,并在aspx会遍历结果。我发现,我总是得到的datacontext会导致无效的尝试调用read时读取器已关闭,因为我换一个使用的范围内我的数据上下文用法错误。如果我不scpope一切用一个子句中正常工作。这是为什么?

My MVC app would call this method from a model class, and the aspx would iterate through the results. I found that I always get an error "datacontext causes Invalid attempt to call Read when reader is closed" because I wrap my data context usage inside a using scope. If I don't scpope everything in a using clause it works fine. Why is this?

我的认为的这未必是一个LINQ或MVC的东西(但不知道是肯定的),是使用条款导致处置中的所有对象之前调用返回?或者,也许选择子句仅执行作为普查员正在通过类似迭代到收益率是如何工作的?

I think this is not necessarily a linq or mvc thing (but don't know for sure), is the using clause causing the dispose to be called before all the objects are returned? Or maybe the select clause is only executing as the enumerator is being iterated through similar to how yield works?

推荐答案

LINQ到SQL使用的工作模式的单元封装访问哪些关于处置(使用范围的结束)关闭连接到数据库的数据库,其原因当你不把它包工程的语句是enumarating查询(可能是坏的,因为它可能会导致剩下的开放连接),它抛出,因为当你第一次使用<$ C只会发生在执行时的上下文是还活着$ C>的IEnumerable 这可能是下了线就认为某个地方,你需要做的是利用改造的IEnumerable 来列表了ToList()这将迫使执行,而不是立即延缓它,以便连接将关闭,你将有你的收藏。

Linq to Sql uses the unit of work pattern to encapsulate access to the database which on dispose (end of using scope) closes the connection to the database, the reason it works when you dont wrap the statement is the context is still alive when enumarating the query (which may be bad as it may lead to the connection remaining open), it throws because the execution will only occur when you first use the IEnumerable which may be somewhere down the line as far as the view, what you need to do is transform the IEnumerable to a list using ToList() which will force execution immediately instead of delaying it so the connection will close and you will have your collection.

这篇关于处置的DataContext导致无效试图调用时,请阅读阅读器关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆