unitofwork例外:可以关闭,连接处于状态连接

发布于 2025-02-05 08:23:59 字数 1439 浏览 2 评论 0原文

在我的项目中,我正在使用

.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.

enter image description here

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.

enter image description here
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文