In all the examples I see for Entity Framework data access, every method has its own using
block, as shown below.
Is there an alternative to this approach? For example, can the context object just be a class member, such as:
MyModelContext context = new MyModelContext();
Is there a reason why a new context object has to be created for each method in the DAO class?
public class DaoClass
{
public void DoSomething()
{
using (var context = new MyModelContext())
{
// Perform data access using the context
}
}
public void DoAnotherThing()
{
using (var context = new MyModelContext())
{
// Perform data access using the context
}
}
public void DoSomethingElse()
{
using (var context = new MyModelContext())
{
// Perform data access using the context
}
}
}