Internal .NET Framework Data Provider error 1 in EF

36 views Asked by At

I'm working for a client on an already existing project. The project is a mix of EF and ASP.NET Core 6.

The web application uses EF to interact with the database. The Unit Of Work and Repository patterns are used. But they have been written in a bit other way. E.g.

public class UnitOfWork: IUnitOfWork, IDisposable
{
    public IDispatchDataContext Context {get;private set;}

    public UnitOfWork()
    {
        Context = new DispatchDataContext();
    }
    
    // Dispose method implemented as per dispose pattern
    // Not showing here for brevity reason
}

public class ClaimRepository
{
    private readonly IUnitOfWork _unitOfWork;
    public ClaimRepository(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }

    public List<SomeClass> GetItems()
    {
        // Dummy code line for explanation purpose only
        return _unitOfWork.Context.Claims.ToList();
    }

    public List<SomeClass> GetItems()
    {
        // Sometimes they initialize and then use
        var uow = new UnitOfWork();
        return uow.Context.Claims.ToList();
    }
}

Now intermittently I'm getting errors as An error occurred while closing the provider connection. See the inner exception for details.

Exception detail

The stack trace shows the details below.

Exeption stack trace

So it seems that EF is trying to release the connection and failing for some reason.

Now, if you will notice the call is made from one repository to another repository. These all repositories use the same instance of UnitOfWork as passed in the first repository's constructor.

This project was working fine in ASP.NET MVC. But after porting to ASP.NET Core it started throwing weird errors. I suspect that it is all due to repositories not being injected by the ASP.NET Core DI container.

Second, wherever UnitOfWork is being used it's not being disposed of using the using keyword.

I know I can try configuring bindings and injecting each repository using DI. But it's a complex project. So there are many classes and repositories to bind and inject. Plus there is no guarantee that injecting the repositories would solve the issue.

I would like to hear your thoughts about resolving this issue.

0

There are 0 answers