From performance viewpoint it doesn't matter. Compared to the actual database interaction creating a context instance is a very fast operation.
However, you should dispose the created context in any case because it holds a database connection as a native resource.
If your want to work with a context member in your DbHelper
class this class should implement IDisposable
so that you can dispose the context when the DbHelper
instance itself gets disposed:
public class DbHelper : IDisposable
{
private MyEntities ent;
public DbHelper()
{
ent = new MyEntities();
}
public void Foo()
{
//...
}
public void Bar()
{
//...
}
public void Dispose() // implementation of IDisposable
{
ent.Dispose();
}
}
You could use this class in a using
block then:
using (var helper = new DbHelper())
{
helper.Foo();
helper.Bar();
} // helper and helper.ent gets disposed now