Is there any "best practice" on how to handle the Datacontext in Entity Framework 4?
Currently, I'm doing something like this in every window:
public class MyWindow()
{
MyEntities() _entities;
MyWindow()
{
_entities = new MyEntities();
InitializeComponent();
}
}
and then the loading into a datagrid like this:
myGrid.ItemsSource= _entities.MyTable;
This is not really clever, because ErrorHandling is made impossible on that.
I'd rather go for a Connection-Class and do something like this:
public class MyData()
{
public IQueryable<Product> GetAllProducts()
{
using(MyContext context = new MyContext())
{
return context.Products;
}
}
}
But first, this creates a new context-instance every time - isn't this bad?
Or should I rather handle a global static DataContext in my Connection-Class? Doesn't get this static variable very overloaded?
public class MyData()
{
private static MyContext Context = new MyContext();
public IQueryable<Product> GetAllProducts()
{
return Context.Products;
}
}