I am building an ASP.NET 4.0 MVC 2 app with a generic repository based on this blog post.
I'm not sure how to deal with the lifetime of ObjectContext -- here is a typical method from my repository class:
public T GetSingle<T>(Func<T, bool> predicate) where T : class
{
using (MyDbEntities dbEntities = new MyDbEntities())
{
return dbEntities.CreateObjectSet<T>().Single(predicate);
}
}
MyDbEntities is the ObjectContext generated by Entity Framework 4.
- Is it ok to call
.CreateObjectSet()and create/disposeMyDbEntitiesper every HTTP request? If not, how can I preserve this object? - If another method returns an
IEnumerable<MyObject>using similar code, will this cause undefined behavior if I try to perform CRUD operations outside the scope of that method?