EFCore在dbcontext的生存期内使用单个dbconnect [英] EFCore use a single dbconnect for the lifetime of dbcontext

查看:293
本文介绍了EFCore在dbcontext的生存期内使用单个dbconnect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EFCore将数据传输到后端db,我对添加到数据集中的每一定数量的新对象调用 SaveChanges 记录它会关闭连接,并在每次调用 SaveChanges 时打开一个新连接:

I'm using EFCore to steaming data to backend db, I call SaveChanges for every a certain number of new objects added to the dataset, I noticed from the EFCore debug log that it will close the connection and open a new one each time I call SaveChanges:

Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerConnection|DEBUG|Opening connection to database ...
Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerConnection|DEBUG|Beginning transaction with isolation level 'Unspecified'.
Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerConnection|DEBUG|Committing transaction
Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerConnection|DEBUG|Closing connection to database...
.... the logs repeats forever

因此,是否只需要在<$ c的整个生命周期中使用一个连接$ c> DbContext ?

推荐答案

您并不需要真正改变它的工作方式,或者甚至根本不用担心。默认情况下,SQL Server连接只是放回连接池中,因此实际上不会关闭。打开一个新池将只抓住池中的下一个池。

There's no real need for you to change how this works, or even worry about it at all. By default, SQL Server connections are just put back into a connection pool so in reality it doesn't get closed. Opening a new one will just grab the next available on in the pool.

如果您确实需要,可以通过在连接字符串中设置值来控制池,而我会建议不要这样做,除非您真的知道自己在做什么,否则这些都是使用的主要属性(来自 MSDN ):

You can control the pool if you really want by setting values in the connection string, while I would advise against that unless you really know what you are doing, these are the main properties that are used (from MSDN):


连接生存时间:何时将连接返回到池,将其创建时间与当前时间进行比较,如果该时间跨度(以秒为单位)超过了连接生命周期指定的值,则将销毁该连接。这在群集配置中用于强制正在运行的服务器和刚刚联机的服务器之间实现负载平衡。
零(0)值将导致池化连接具有最大超时。

Connection Lifetime: When a connection is returned to the pool, its creation time is compared with the current time, and the connection is destroyed if that time span (in seconds) exceeds the value specified by Connection Lifetime. This is useful in clustered configurations to force load balancing between a running server and a server just brought online. A value of zero (0) will cause pooled connections to have the maximum time-out.

连接重置:确定是否重置连接从池中删除数据库连接时,将重置数据库连接。对于Microsoft SQL Server 7.0版,设置为false可以避免在获得连接时进行额外的服务器往返,但是您必须知道连接状态(例如数据库上下文)没有被重置。

Connection Reset: Determines whether the database connection is reset when being removed from the pool. For Microsoft SQL Server version 7.0, setting to false avoids making an additional server round trip when obtaining a connection, but you must be aware that the connection state, such as database context, is not being reset.

注册:为true时,如果存在事务上下文,则池管理器会自动在创建线程的当前事务上下文中注册连接。

Enlist: When true, the pooler automatically enlists the connection in the current transaction context of the creation thread if a transaction context exists.

最大池大小:池中允许的最大连接数。

Max Pool Size: The maximum number of connections allowed in the pool.

最小池大小 >:在池中维护的最小连接数。

Min Pool Size: The minimum number of connections maintained in the pool.

池化:如果为true,则从相应的池中提取连接,或者必要,创建并添加到适当的池中。

Pooling: When true, the connection is drawn from the appropriate pool, or if necessary, created and added to the appropriate pool.

这篇关于EFCore在dbcontext的生存期内使用单个dbconnect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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