Linq to SQL DataContext Lifetime Management Issue

I read an article by Rick Thrall article about how to solve 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 private class in the 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 refer to it as follows

DataContext dc = DataContext.Current;

However, this causes problems when I update my DBML file. After editing a DBML file, whenever I try to build a project, my designer file does not regenerate / delete. If I try to launch the custom tool option, it will return with an error.

, , - , , . , .. .

, DBML DC ?

+3
4

DataContext . - , .

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

    partial class DataContext
    {
    }
}

, VS2008 VS2008 SP1, .

+5

, .designer.cs. - DBML ( DBML) " ". .cs, DBML.

+3

, DataContext DBML . , IDE DataContext (HttpContext.Current.Items , ).

DBML, , . , DBML "" ( ) HttpApplicationState.Page.Application.

, DBML.

+3
source

I can't think of an overly compelling reason why your static property should be part of the DataContext class. It would be easy to have him in another class.

0
source

Source: https://habr.com/ru/post/1708900/


All Articles