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 126753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:22:04+00:00 2026-05-11T05:22:04+00:00

The idea is to create a class that exposes a context but handles the

  • 0

The idea is to create a class that exposes a context but handles the storage of it in a web application.

Currently this is what I have:

public class EntityContext {      private static String MAIN_CONTEXT_KEY = 'MainContext';     private static TISQLEntities _context;      public static void RemoveContext()     {         if (             HttpContext.Current != null              &&              HttpContext.Current.Items[MAIN_CONTEXT_KEY] != null            )         {             ((TISQLEntities)HttpContext.Current.Items[MAIN_CONTEXT_KEY]).Dispose();             HttpContext.Current.Items[MAIN_CONTEXT_KEY] = null;         }          if (_context != null)         {             _context.Dispose();             _context = null;         }     }      public static TISQLEntities Context     {         get         {             if (HttpContext.Current == null)             {                 if (_context == null)                 {                     _context = new TISQLEntities();                 }                  return _context;             }              if (HttpContext.Current.Items[MAIN_CONTEXT_KEY] == null)             {                 HttpContext.Current.Items[MAIN_CONTEXT_KEY] = new TISQLEntities();             }              return (TISQLEntities)HttpContext.Current.Items[MAIN_CONTEXT_KEY];         }     } } 

And then in the Global.asax file:

protected void Application_EndRequest(object sender, EventArgs e) {     EntityContext.RemoveContext(); } 

The idea is that if this is being run with a web application, the context is created on first need (and saved to the current HttpContext) and torn down whenever the request is over.

If this is a UnitTest situation it is against created on first need and removed in the TestCleanup (Not as important in this post but just wanted to clarify the _context object).

Now the idea behind this is in the least to not have to do this:

using(TISQLEntities context = new TISQLEntities()) {   .... } 

Everytime I want to query. I realize this may be me being lazy, but I just think that it’s easier and cleaner to have:

EntityContext.Context.User.Select(...) 

And avoids ‘using’ which I try to avoid for most cases. On top of that, I’m not creating 9001 contexts per postback.

Now what I am curious about is that am I over thinking this? Should I just keep creating a context for every method that needs one? Say on a post back I have to:

  • Get the user from an ID
  • Get a site from an id
  • Add the Site to the User (user.Site = foundSite)
  • Save the user

That could entail at least 3 contexts. Is entity framework smart enough that it’s ok to just keep creating contexts whenever?

  • 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. 2026-05-11T05:22:05+00:00Added an answer on May 11, 2026 at 5:22 am

    You are implementing the equivalent of NHibernate’s session per request pattern which is a good construct in NHibernate. While I can’t say 100% for sure that it’s applicable to EF it most likely is. Further expanding on other session management patterns is the Session per Business Conversation which allows NHibernate to extend holding a session over the duration of a HttpSession by disconnecting and reconnecting the session as opposed to destroying and creating. If the EF allows a similar ability as opposed to keeping a static open connection you could look at how I implemented that pattern using AOP on my blog through my profile.

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

Sidebar

Ask A Question

Stats

  • Questions 258k
  • Answers 258k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer never tried it myself but you should be able to… May 13, 2026 at 11:00 am
  • Editorial Team
    Editorial Team added an answer Take a look at TMS AdvSmoothImageListBox: AFAIK, Registered Delphi customers… May 13, 2026 at 11:00 am
  • Editorial Team
    Editorial Team added an answer register gives the compiler a hint to place the variable… May 13, 2026 at 11:00 am

Related Questions

I'm writing a .NET library to inject managed DLLs into external processes. My current
DTO I'm building a Web application I would like to scale to many users.
I'm writing a small visualization tool in wpf, the idea is that average users
I've got a group of inter-related classes that are all overridden together to create

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.