EF: Should I explicitly close the database connection when manually calling OpenConnection

I open the connection in the constructor. Consider this code:

public abstract class DataContext : DbContext, IDataContext
{
    static DataContext()
    {
        if (DataContextConfiguration.UseSafePersian)
        {
            DbInterception.Add(new SafePersianInterceptor());                
        }
    }

    private readonly bool _saveChangesOnModify = false;

    protected DataContext(string nameOrConnectionString)
        : base(nameOrConnectionString)
    {
        this.OpenConnection();
    }
   internal void OpenConnection()
        {
            if (((IObjectContextAdapter)this).ObjectContext.Connection.State != ConnectionState.Open)
                ((IObjectContextAdapter)this).ObjectContext.Connection.Open();
        }
    }

Should I close the connection when I open the connection openly?

I am using Entity Frameworkversion 6.

UPDATE

I got this error, and this happens very often:

Timed out. Waiting period before receiving connection to the pool. Perhaps this happened because all pooled connections were used and the maximum pool size was reached.

+4
source share
1 answer

EF / . , , EF .

, . , DbContext ( ).

, , DbContext (, ). , .

EF MSDN.

+5

All Articles