更新Asp.net应用Npgsql 3和删除preLOAD阅读器 [英] Update Asp.net app to Npgsql 3 and removing Preload Reader

查看:255
本文介绍了更新Asp.net应用Npgsql 3和删除preLOAD阅读器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经更新了我的ASP.NET应用程序从NpgSQL 2.2.5至3.0.1。在重大更改它的规定,他们已经删除了的 preLOAD阅读的支持。所以,我从字符串连接将其删除。
测试我的web应用程序,我得到了错误的操作已在进行中。特别是在 LINQ查询的是这样的:

I have updated my ASP.NET app from NpgSQL 2.2.5 to 3.0.1. In the breaking changes it's specified that they have removed the Preload Reader support. So I remove it from the string connection. Testing my web app, I got the error "An operation is already in progress." specially in the linq query like this:

 var plugins =
            from p in _pluginRepository.GetPlugins() // this method return this: GetAll().OrderBy(p => p.Created)
            join e in _userPluginRepository.GetByUserId(user.Id).ToList() on p.Id equals e.Plugin.Id into pe
            from e in pe.DefaultIfEmpty()
            select new PluginViewModel
            {
                Active = e != null,
                Name = p.Translations.ToUserLanguage(loggedInUser),
                Key = p.Key,
                PluginId = p.Id,
                SettingId = e == null ? 0 : e.Id,
                ExpireDate = e != null && e.ExpireDate.HasValue ? e.ExpireDate.Value : (DateTime?) null,
                Grants = e == null ? UserPluginGrants.None.GetHashCode().ToString() : e.Grants.GetHashCode().ToString()
            };

要解决这个错误,我要追加的了ToList 的的的 GetPlugins 的方法之后。
这是正确的行为,而使用的 preLOAD阅读的?为什么呢?

To solve this error, I have to append a ToList after the GetPlugins method. Is this the correct behavior to use without Preload Reader? Why?

推荐答案

在Npgsql 2.x中,使用preLOAD读卡器,Npgsql拉动整个结果集从数据库中查询的到你的应用程序的内存。这释放连接,并允许同时仍然穿越第一次查询的结果集要执行其他命令。换句话说,它允许您编程好像可以并发执行多个查询(有时也被称为MARS),虽然在幕后这是低效执行。

In Npgsql 2.x, using the Preload Reader made Npgsql pull the entire result set of the query from the database into the memory of your application. This freed the connection and allowed another command to be executed while still traversing the resultset of the first query. In other words, it allowed you to program as if you could execute multiple queries concurrently (known sometimes as MARS), although behind the scenes this was implemented inefficiently.

添加了ToList()不完全一样的东西 - 拉一切都变成客户端内存中,只有它发生在你的应用程序code,而不是在数据库驱动程序。所以,这绝对是港口从Npgsql 2.x到3.x的应用程序能够接受的方式。

Adding a ToList() does exactly the same thing - pull everything into client memory, only it happens in your application code instead of in the database driver. So it's definitely an acceptable way to port your application from Npgsql 2.x to 3.x.

现在,如果结果集被拉出(在这种情况下GetPlugins)是小的,这是一个非常有效的方法。如果是大了,但是你应该考虑替代方案。在你的榜样,可以将连接发送到数据库中,使您的LINQ的前pression转换成一个SQL查询,并消除对多个查询(奥姆斯如实体框架通常可以为你做这个)的需要。一个更极端的解决办法是使用多个数据库连接,但更重,也是有问题的,如果你使用的交易。

Now, if the result set being pulled (in this case GetPlugins) is small, this is a perfectly valid approach. If it's big, however you should look into alternatives. In your example, the join could be sent to the database, making your Linq expression translate into a single SQL query and eliminating the need for multiple queries (ORMs such as Entity Framework can usually do this for you). A more extreme solution would be to use multiple database connections, but that is heavier and also problematic if you're using transactions.

需要注意的是有一个开放的问题在Npgsql实现真正的MARS(虽然不太可能很快实现):的 https://github.com/npgsql/npgsql/issues/462

Note that there's an issue open for implementing true MARS in Npgsql (although it isn't likely to be implemented very soon): https://github.com/npgsql/npgsql/issues/462

这篇关于更新Asp.net应用Npgsql 3和删除preLOAD阅读器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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