Do injected resources automatically dispose in EF6?

Stesvis 1,041 Reputation points
2021-10-12T00:27:44.107+00:00

Hello,
I am wondering if injected resources in EF6 automatically dispose or not.

For example I use UnitOfWork in my project:

public interface IUnitOfWork : IDisposable
{
    //...
}

Then I have:

public class UnitOfWork : IUnitOfWork
{
    //...

        private bool _disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    _context.Dispose();
                }
            }
            _disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

}

Now in my services or controllers, I use the UnitOfWork in the following way, to properly dispose it:

public class MyService : IMyService
{
    public void MyMethod()
    {
        using (var unitOfWork = new UnitOfWork())
        {
            unitOfWork.DoSomething();
        }
    }

}

However, my preferred method would be to inject the UnitOfWork like this:

public class MyService : IMyService
{
    private IUnitOfWork _unitOfWork;

    public MyService(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }

    public void MyMethod()
    {
        _unitOfWork.DoSomething();
    }

}

In this last scenario, will _unitOfWork be automatically/safely disposed? I would tend to say yes, but I don't want to risk leaving hanging resources...
Please note that I am using MVC5 and not .NET Core (should they behave differently), and that UnitOfWork is registered as singleton:

unityContainer.RegisterSingleton<IUnitOfWork, UnitOfWork>();

Thank you!

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
694 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,327 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,026 Reputation points
    2021-10-16T09:59:22.803+00:00

    Both Entity Framework 6 and Entity Framework Core when accessed as shown below

    using (var context = new NorthwindContext())
    {
        var customers = context.Customers.ToList();
    }
    

    .

    Or with .NET Core 5/C#9 and higher

    using var context = new NorthwindContext();
    var customers = context.Customers.ToList();
    

    Will dispose of any scoped objects. Now if say your DbContext is scoped private in a class you need to dispose of it else if the app closes it will then dispose of it's resources.

    Best practice is to wrap your code for EF in a using statement.

    There are fringe cases were developer believed they had memory leakage yet usually a code rewrite resolve these issues.

    Also, unless there is a compelling reason to use EF 6 consider moving to EF Core which has many improvements. Consider EF Core 6 which is a RC and due for proper release the second week of November of this year.