Considering this class
public class XQueries
{
public IQueryable Query1()
{
using (XEntities context = new XEntities())
{
return something;
}
}
public IQueryable Query2()
{
using (XEntities context = new XEntities())
{
return somethingElse;
}
}
}
Is a connection to the database created for every (XEntities context = new XEntities()) {...} ? If so what is the correct way to create a static UnitOfWork class so that only 1 connection to exist?
You can't create a static unit of work, because by definition a unit of work is a short lived object. Because the EF
ObjectContext
is designed around the unit of work pattern it is a bad idea to have a singleObjectContext
instance during the life time of the application. There are several reasons for this.First of all, the
ObjectContext
class is not thread-safe. This means that during the unit of work of one user (in a web app for instance), another user can commit his unit of work. When they share the sameObjectContext
, it means that in that situation just half of the changes are persisted and changes are not transactional. When you are lucky theObjectContext
fails and throws an exception. When you are unlucky, you corrupt theObjectContext
and safe and load crap from and to your database and find out when your application is running in production (of course, during testing and staging everything always seems to work).Second, the
ObjectContext
has a caching mechanism that is designed for it to be short lived. When an entity is retrieved from the database it stays in theObjectContext
’s cache until that instance is garbage collected. When you keep that instance alive for a long period of time, entities get stale. Especially if that particularObjectContext
instance is not the only one writing to that database.