Q:

How can I close connection string object in Entity Framework

Today I got an exception when I run my application from the below code
dbContext.ManageTasks.Where(x => x.IsDeleted == false).ToList();

Error is 

Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

I got a solution from this discussion : How can I solve a connection pool problem between ASP.NET and SQL Server? 
Solution is : if I close current connection string object, then this error will be gone. 
SqlConnection myConnection = new SqlConnection(ConnectionString);
myConnection.Open();
// some code
myConnection.Close();        

The above code for ADO.NET, not Entity Framework.
How can I close the connection string object in Entity Framework? 

A:

You are probably keeping a global variable dbContext somewhere in your class. You should not do that!
You could use the IDisposable interface that DbContext implements and use using statements. You initialize a new DbContext whenever you need one:
using (YourDbContext dbContext = ...)
{
    // do some actions
}

// context will be closed

The benefit of using using is that it will close and dispose, even if the code inside will throw an exception.

A:

I'm a bit unsure as to why you would need the use of "SqlConnection". DbContext is a disposable class. I found the safest way to clean up is by using the "using" tag. E.g.
using(DbContext _context = new MyContext())
{
     \\Your Work here
}

This will automatically call _context.Dispose() when it reaches the closing curly bracket which in effect closes your connection.
The alternative would be:
DbContext _context = new MyContext();
_context.ManageTasks.Where(x => x.IsDeleted == false).ToList();
_context.Dispose();

This does mean it's possible to miss the dispose call if an exception occurs.
Hope this helps.
Useful Link
In order to handle transaction scope and to help dispose you objects, you may want to look at the repository pattern and the unitOfWork pattern. Or both.

