简体   繁体   English

如何关闭实体框架中的连接字符串对象

[英]How can I close connection string object in Entity Framework

Today I got an exception when I run my application from the below code今天,当我从下面的代码运行我的应用程序时出现异常

dbContext.ManageTasks.Where(x => x.IsDeleted == false).ToList();

Error is错误是

Timeout expired.超时已过。 The timeout period elapsed prior to obtaining a connection from the pool.在从池中获取连接之前超时时间已过。 This may have occurred because all pooled connections were in use and max pool size was reached.这可能是因为所有池连接都在使用中并且达到了最大池大小。

I got a solution from this discussion : How can I solve a connection pool problem between ASP.NET and SQL Server?我从这个讨论中得到了一个解决方案: 如何解决 ASP.NET 和 SQL Server 之间的连接池问题?

Solution is : if I close current connection string object, then this error will be gone.解决方案是:如果我关闭当前的连接字符串对象,那么这个错误就会消失。

SqlConnection myConnection = new SqlConnection(ConnectionString);
myConnection.Open();
// some code
myConnection.Close();        

The above code for ADO.NET, not Entity Framework.上面的 ADO.NET 代码,而不是实体框架。

How can I close the connection string object in Entity Framework?如何关闭实体框架中的连接字符串对象?

I'm a bit unsure as to why you would need the use of "SqlConnection".我有点不确定您为什么需要使用“SqlConnection”。 DbContext is a disposable class. DbContext 是一个一次性类。 I found the safest way to clean up is by using the "using" tag.我发现最安全的清理方法是使用“使用”标签。 Eg例如

using(DbContext _context = new MyContext())
{
     \\Your Work here
}

This will automatically call _context.Dispose() when it reaches the closing curly bracket which in effect closes your connection.当它到达实际上关闭您的连接的结束大括号时,这将自动调用 _context.Dispose()。

The alternative would be:替代方案是:

DbContext _context = new MyContext();
_context.ManageTasks.Where(x => x.IsDeleted == false).ToList();
_context.Dispose();

This does mean it's possible to miss the dispose call if an exception occurs.这确实意味着如果发生异常,可能会错过 dispose 调用。

Hope this helps.希望这可以帮助。

Useful Link有用的链接

In order to handle transaction scope and to help dispose your objects, you may want to look at the repository pattern and the unitOfWork pattern.为了处理事务范围并帮助处理对象,您可能需要查看存储库模式和 unitOfWork 模式。 Or both .或者两者兼而有之

Further to this, I know use DependencyInjection to handle a lot of connections.除此之外,我知道使用 DependencyInjection 来处理大量连接。 I just ensure they are Scoped (per web request) :)我只是确保它们是 Scoped(每个 Web 请求):)

You are probably keeping a global variable dbContext somewhere in your class.您可能在类中的某处保留了一个全局变量dbContext You should not do that!你不应该那样做!

You could use the IDisposable interface that DbContext implements and use using statements.您可以使用DbContext实现的IDisposable接口并使用using语句。 You initialize a new DbContext whenever you need one:只要需要,就初始化一个新的DbContext

using (YourDbContext dbContext = ...)
{
    // do some actions
}

// context will be closed

The benefit of using using is that it will close and dispose, even if the code inside will throw an exception.使用using的好处是它会关闭和处理,即使里面的代码会抛出异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM