I open connection in 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 connection when I open connection explicitly?
I use Entity Framework
version 6.
UPDATE
I got this error and it happens very often:
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.