用于Postgresql和ado.net实体数据模型的MultipleActiveResultSets [英] MultipleActiveResultSets for postgresql and ado.net entity data model

查看:114
本文介绍了用于Postgresql和ado.net实体数据模型的MultipleActiveResultSets的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio,PostgreSQL数据库和ado.net实体数据模型。在连接字符串中,我无法设置 MultipleActiveResultSets = True

Im using visual studio, postgresql database and ado.net entity data model. In the connectionstring, Im unable set MultipleActiveResultSets=True.

通常,当我使用<$ c连接到sql server时$ c> MultipleActiveResultSets = True ,它可以正常工作。

Usually when I connect to sql server with MultipleActiveResultSets=True, it works fine. but i cannot set the same with postgresql database.

当我使用它时,出现以下错误

When I use this, I got the following error


已经有与此命令关联的打开的DataReader,必须首先关闭

There is already an open DataReader associated with this Command which must be closed first.

如何解决此问题。

推荐答案

多个活动结果集(MARS)是SQL Server 2005中引入的一项功能,不是在其他数据库系统(例如postgres)中可用,因此您将无法在连接字符串中将其打开。

Multiple Active Result Sets (MARS) is a feature introduced in SQL Server 2005 and is not available in other database systems like postgres so you won't be able to turn it on in the connection string.

您面临的错误是尝试执行的结果在一个打开的数据读取器上进行两个查询。使用Entity Framework时,通常会在您打开延迟加载并且将延迟属性加载到与父实体相同的读取器中时发生。例如,与此类似的代码可能会产生此错误:

The error you are facing is an outcome of trying to perform two queries on one open data reader. When using ie Entity Framework this usually happens when you have Lazy Loading turned on and the lazy properties are loaded in the same reader as the parent entites. For example a code similiar to this could produce this error:

var users = context.Users.Where(u => u.FirstName.StartsWith("Ha"));
foreach (var user in users)
{
    Console.WriteLine(user.Address.StreetName);
}

在第一行中,由于仅准备了Linq查询,因此未获取任何数据。当我们启动 foreach 时,将打开 DataReader ,并查询满足我们条件的用户集合,但读取器是没有关闭。然后在 foreach 内,我们到达延迟加载的User的Address属性。这种延迟加载会导致在同一打开的 DataReader 上执行查询,而这正是发生异常的时候。如果我想摆脱错误,我可以简单地在行的末尾添加 ToList()(或任何导致查询执行的内容):

In the 1st line no data is fetched as we only have prepared a Linq query. When we start the foreach a DataReader is opened and collection of users that meets the our conditions is queried but the reader is not closed. Then inside foreach we reach to the Address property of User which is lazy loaded. This lazy load causes a query execution on the same open DataReader and that's when the exception occurs. If i wanted to get rid of the error i could simply add a ToList() (or anything causing the query to perform) to the end of the line like this:

var users = context.Users.Where(u => u.FirstName.StartsWith( Ha))。ToList();

希望这对您有所帮助。

这篇关于用于Postgresql和ado.net实体数据模型的MultipleActiveResultSets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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