views:

670

answers:

4

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 ?

+3  A: 

You should create your partial class in a different file, not the .designer.cs file. The easiest way to do this is to right-click on your DBML in the solution explorer (or in a blank area in the DBML designer) and click "View Code". This will create a new .cs file that won't be overwritten when you save your DBML.

Jaecen
Forgot to mention that the partial class is in a separate file.
Jon Jones
If you name your partial file something like MyContext.partial.cs (instead of MyContext.cs, which would match the name of the main MyContext.dbml) then the IDE won't associate your partial file with the main dbml file and you can then format your code any way you like.
wizlb
+3  A: 

I don't believe your DataContext persistence and DBML issue are related. It sounds like the IDE is confused versus a conflict with a Cached DataContext (HttpContext.Current.Items is per Request so there's no long-term caching anyway).

I had trouble with DBML compilation when my data model contained a class name that conflicted with another class. For instance, a DBML object named 'Application' (an insurance application) might conflict with HttpApplicationState.Page.Application.

Check your error message and see if it's specific to a name in your DBML.

Corbin March
+4  A: 

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.

namespace MyNamespace
{
    using System;
    using System.Data.Linq;
    using System.Data.Linq.Mapping;
    using System.Reflection;
    using System.Xml.Linq;

    partial class DataContext
    {
    }
}

I believe this change was required when moving from VS2008 to VS2008 SP1, though I may be mixing up some versions.

Ryan Versaw
worked like a charm. Thanks!
Jon Jones
A: 

I can't think of an overly compelling reason why your new static property must be part of the DataContext class. It would be just as easy to have it in a different class.

Dinah