unitofwork例外:可以关闭,连接处于状态连接
在我的项目中,我正在使用
.NET 6 炸弹服务器侧:v6.0.10 EF核postgresql:V 6.0.4 和单位工程模式。
在 index.Razor页面我正在加载数据库中的一些数据,这需要几秒钟。
在我离开剃须刀页面的这段时间里在状态连接中。
这是我的单位工人类:
public class UnitOfWork : IUnitOfWork, IDisposable
{
private readonly FMDataContext _context;
// SOME REPOSITORYS
private bool _disposed;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
if (disposing)
_context.Dispose();
_disposed = true;
}
}
有人想知道如何解决此问题?如何将dbcontext处置在连接状态?
此代码可以解决问题,但是只有在不在连接状态下处置上下文是正确的吗?
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
if (_context.Database.GetDbConnection().State != ConnectionState.Connecting)
{
_context.Dispose();
}
}
}
this.disposed = true;
}
in my project i'm using
.NET 6
Blazor-Server-Side : V6.0.10
EF-Core PostgreSQL: V 6.0.4
and a UnitOfWork pattern.
On
the index.razor page i'm loading some data from the database this take a few seconds.
In this time when i leave the razor page for example to login with discord the page trigger the Dispose function fom my unitofworker class and i get the error message : Can't close, connection is in state Connecting.
This happen only when i leave the razor page before the data are loaded.
Here is my unitofworker class:
public class UnitOfWork : IUnitOfWork, IDisposable
{
private readonly FMDataContext _context;
// SOME REPOSITORYS
private bool _disposed;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
if (disposing)
_context.Dispose();
_disposed = true;
}
}
Someone an idea how to fix this issue? How can dispose the dbcontext in connecting state?
This code can fix the issue but is this correct to dispose the context only when not in connection state?
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
if (_context.Database.GetDbConnection().State != ConnectionState.Connecting)
{
_context.Dispose();
}
}
}
this.disposed = true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论