I read Rick Strahl’s article about ways to deal with the data context. My DBML is inside a class library, I keep my data context open by creating a static Current method in a separate custom partial class within library.
public partial class DataContext
{
public static DataContext Current
{
get
{
DataContext dc = HttpContext.Current.Items["dc"] as DataContext;
if (dc == null)
{
dc = new ImmediacyPageDataContext();
HttpContext.Current.Items["dc"] = dc;
}
return dc;
}
}
then access it like this
DataContext dc = DataContext.Current;
However, this causes issues whenever I update my DBML file. After editing the DBML file whenever I try and build the project my designer file doesn’t regenerate/gets deleted. If I try running the custom tool option it comes back with an error.
The only way I can get round this is by renaming or deleting the custom partial class, re-generating the designer file, then adding my custom partial class back into the solution. This is a does work, but.. its a bit of a pain.
Is there a better approach to take that will make editing my DBML files easier, while prolonging my DC for as long as possible ?
Go into the code file with your partial DataContext class and move your using statements into your namespace. For some reason the tool will not generate the designer unless this is the case.
I believe this change was required when moving from VS2008 to VS2008 SP1, though I may be mixing up some versions.