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

  • SEARCH
  • Home
  • 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 8070691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:28:59+00:00 2026-06-05T13:28:59+00:00

I have problem with consuming DbContext class in EF 4.3 CodeFirst approach. As for

  • 0

I have problem with consuming DbContext class in EF 4.3 CodeFirst approach.

As for now I have created repository that contains all methods needed to get/put things from/into database. The problem is that all of that methods must wrap around a code like this:

  using(var context = new MyDbContext())
  {
    //some code here to run on database
  }

I have, however, no idea how to refactor this to single class that does all of initialization and disposes after each query. I have also heard that I shouldn’t use static field for DbContext (which I eventually did and it worked, but also not elegant solutio). I have not found any constructive info on web as well as no book explains how to do this in elegant way.

Thank you in advance for help.

  • 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-06-05T13:29:01+00:00Added an answer on June 5, 2026 at 1:29 pm

    Try to use the approach called “DbContext per Request”. You basically create an instance of DbContext on demand, attach it to HttpContext and then you check during Application_EndRequest if the instance exists and if yes, dispose of it.

    RequestContext.cs

    internal static class RequestContext
    {
        internal static MyDbContext Current
        {
            get
            {
                if (!HttpContext.Current.Items.Contains("myContext"))
                {
                    HttpContext.Current.Items.Add("myContext", new MyDbContext());
                }
                return HttpContext.Current.Items["myContext"] as MyDbContext;
            }
        }
    }
    

    Global.asax.cs

    protected void Application_EndRequest(object sender, EventArgs e)
    {
         var entityContext = HttpContext.Current.Items["myContext"] as MyDbContext;
         if (entityContext != null)
             entityContext.Dispose();
    }
    

    Example usage:

    public class MyDbAccessClass
    {
        public static List<Products> GetProducts()
        {
             var products = from o in RequestContext.Current.Products
                            select o;
             return products.ToList();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hey all i have a problem in webservice that i am consuming. it takes
I have a problem that has been consuming me for days. I have a
I have an inner class that downloads some images from the server. The problem
I have a problem with consuming a third-party web service in .NET C#. It
I'm having a really confusing problem. I have a UIWebView that is contained within
I have problem with fancybox. I want to write a function that will run
i have problem with autorotate on iphone i set up in all classes -
I have problem with Oracle 9.2 and JMS. I created PL/SQL routine to send
I have problem consuming a WCF REST service in my android application. The client
I have a socket wrapper class that sends and recieves messages with a server

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.