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

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

问题描述

我正在使用 EFCore 将数据传输到后端数据库,我为添加到数据集中的每一定数量的新对象调用 SaveChanges,我从 EFCore 调试日志中注意到它将关闭每次我调用 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

那么有没有办法在 DbContext 的整个生命周期中只使用一个连接?

So is there anyway to just use one connection for the entire lifecycle of a 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):

Connection Lifetime:当一个连接返回到池中时,它的创建时间与当前时间进行比较,如果该时间跨度(以秒为单位)超过指定的值,则连接被销毁连接寿命.这在集群配置中很有用,可以在正在运行的服务器和刚刚上线的服务器之间强制进行负载平衡.零 (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.

Connection Reset:确定数据库连接从池中移除时是否重置.对于 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.

Enlist:如果为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:池中维护的最小连接数.

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

池化:如果为真,则连接从适当的池中提取,或者在必要时创建并添加到适当的池中.

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天全站免登陆