Is it necessary to restrict Entity Framework access by singleton?
Within the realm of Entity Framework, if I have 2 seperate classes leveraging a data model, do I need to ensure that there is only 1 reference to the entities object (singleton or similar)? In other words, in the scenario bel开发者_如何学JAVAow, is there any danger?
public class Foo
{
public void DoSomething()
{
using (MyEntities entities = new MyEntities())
{
//use _entities in some fashion here
}
}
}
public class Bar
{
public void DoSomething()
{
using (MyEntities entities = new MyEntities())
{
//use _entities in some fashion here
}
}
}
Thanks.
Here's a blog post which you might find interesting about lifetime of data contexts.
精彩评论