开发者

LINQ To SQL error "There is already an open DataReader associated with this Command"

There is already an open DataReader associated with this Command

I am getting this开发者_如何学Go error when I retrieve data from the DataContext object.

How can this be fixed?


Ensure that you're not declaring your DataContext as static. Create and destroy your DataContext on each use.

public MyDataClass{

    CustomerDataContext db;

    public void MyDataClass()
    {
       db = new CustomerDataContext();
    }

    public Customer GetCustomer(int id)
    {
       return db.Customers.SingleOrDefault(c=>c.ID == id);
    }
} 


Check this post

Is mixing ADO.NET and LINQ-TO-SQL bad? My data layer isn't working

It realy depends how you store, access and dispose the datacontext. Try to reproduce the error using a load test tool. I use jmeter. Lots of people don't know that they have this issue, because they have too little traffic.


You have forget to close the DataReader, and you start one more DataReader on the same connection.


What helped me was converting all the IQueryable types associated with the query that is throwing the error to more native types before using them in another query.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜