Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7525601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:35:48+00:00 2026-05-30T03:35:48+00:00

I use the following, static class to access the data context in my application

  • 0

I use the following, static class to access the data context in my application

public static class DataContext
{
    internal const string _contextDataKey = "dataContext";

    /// <summary>
    /// Returns a unique data context that lives for the duration of the request, which can be from ASP.NET or a WCF service
    /// </summary>
    /// <returns>The entity data model context for the current request</returns>
    public static EntityDataModelContext GetDataContext()
    {
        IPersistanceContainer state;

        if (HttpContext.Current != null)
        {
            state = new AspNetPersistanceContainer();
        }
        else if (OperationContext.Current != null)
        {
            state = new WcfPersistanceContainer();
        }
        else
        {
            state = new StaticPersistanceContainer(); // this container is thread-unsafe.
        }

        EntityDataModelContext edm = state.Get<EntityDataModelContext>(_contextDataKey);
        if (edm == null)
        {
            edm = new EntityDataModelContext();
            state.Store(_contextDataKey, edm);
        }

        return edm;
    }
}

Forget about the other containers, which are for WCF and Console application simple-tests respectively, here’s the ASP.NET container:

internal class AspNetPersistanceContainer : IPersistanceContainer
{
    public T Get<T>(string key) where T : class
    {
        if (HttpContext.Current.Items.Contains(key))
            return (T)HttpContext.Current.Items[key];

        return null;
    }

    public void Store(string key, object value)
    {
        HttpContext.Current.Items[key] = value;
    }
}

When I need to access the context I just invoke DataContext.GetDataContext() and do my DB-accessing, I never add any using statements.

If I add a using statement, the context is good for one use, and the next time I try to use it, it’s disposed of. Raising an exception.

If I don’t, like right now, it makes me kind of unhappy, I feel like it’s not the right thing to do either, not disposing of it.

So I was wondering what would be the correct thing to do here.

Is this design flawed, and should I abandon it altogether?

Should I just figure out a way to re-create the context whenever it’s disposed of?

Should I just leave the design as is, and that’s fine?

Maybe the design is “fine enough”, are there any books that you’d recommend I read on the subject? I feel like my skills on back-end architecture are rather on the lacking side.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:35:49+00:00Added an answer on May 30, 2026 at 3:35 am

    In an asp.net application one solution can be like this :

    1. Create your context when a request begins
    2. Dispose it when the request ends

    Here‘s an article that discuss this approach (for NHibernate session management but it’s almost the same for EF )

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to use the following PHP class: <?php class Service { public
Consider the following use of template template parameters... #include <iostream> template <typename X> class
I use the following code try to create an array of string vectors, I
I use C++ to implement a thread class. My code shows in the following.
I want to use pointer magikry to save a C++ class using the following
take two following classes: class Test1{ public: Test1()=default; Test1(char in1,char in2):char1(in1),char2(in2){} char char1; char
I want to use the following code to access the state of the keyboard
I tried to implement a singleton class in the following way (I use VS2008
I am trying to adapt my data model to use a BaseEntity base-class. The
I need to access a static property from a console application (NServiceBus selfhost) while

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.