Entity Framework SQL Connections are not getting closed or disposed

710 Views Asked by At

I'm using Entity Framework with Repository Pattern, but we have requirement to call stored proecedure, so we ended up in creating DAL Class with EF DBContext has local private variable in DAL Class, private context;

I try to instantiate DBContext in Repository Class Constructor

public VRepository( IDataAccessObjectFactory daoFactory)
{
        _daoFactory = daoFactory;
        context = _daoFactory.GetEFContext<DbEntities>();           
}

All the Services call a method of DAL's repository class and all repository class inherits from IDisposable and implemented disposable method in repository class

public void Dispose()
    {
        if (context != null)
        {

            context.Dispose();
            // GC.SuppressFinalize(this);
        }
    }

I try to call above dispose method from service layer after completing Operation in a method, something like calling dispose method of BLL finally block to dispose the DBContext object( I'm having WEB API , WEB API -> BLL -> DAL).

Even after calling context.Dispose(), EF created Sql Connections are not getting Closed. Can Any1 suggest me right way of closing SQL connections and dispose SQL Connection. If I dont dispose, SQL connections goes to 100 which is not allowing new connections for some time or worker restart. Please let me know effective way of dispose context immediately completion of repository layer method.

0

There are 0 best solutions below