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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:21:58+00:00 2026-06-17T19:21:58+00:00

I got ASP.NET MVC application and I’m using EF code first and Unity dependency

  • 0

I got ASP.NET MVC application and I’m using EF code first and Unity dependency injector.
I’ve implemented the DDD design and have repositories and services which interact with my POCO objects.

My problem is that I’m getting EF related problems – such as the connection was closed, the entities changed or not tracked and so on. As I’ve understand from researches in Google, it related to the way that I should configure Unity.

I’ve understood that I need to put it as per request instance, but as I see there’s no built-in strategy in Unity nor in the Unity.Mvc3 package.
I’ve tried to write my own strategy, which solved many problems but I do stuck with the problem that sometimes I get “connection closed”.

My HttpContextLifetimeManager.cs

public class HttpContextLifetimeManager<T> : LifetimeManager, IDisposable
{
    public override object GetValue()
    {
        return HttpContext.Current.Items[typeof(T).AssemblyQualifiedName];
    }

    public override void RemoveValue()
    {
        HttpContext.Current.Items.Remove(typeof(T).AssemblyQualifiedName);
    }

    public override void SetValue(object newValue)
    {
        HttpContext.Current.Items[typeof(T).AssemblyQualifiedName] = newValue;
    }

    public void Dispose()
    {
        RemoveValue();
    }
}

And inside DependencyConfig.cs (MVC 4 App_Start) I’m registering it using:

container.RegisterInstance<MyDbContext>(new MyDbContext(), new HttpContextLifetimeManager<DependencyConfig>());

Can you recommend me an implementation that works/help me fix mine/forward me to an article or a tutorial that can help me solve that problem?

Thanks a lot.

  • 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-17T19:21:59+00:00Added an answer on June 17, 2026 at 7:21 pm

    App_Start method is only called once when the first request comes for the application.

        Called when the first resource (such as a page) in an ASP.NET application is requested.  
     The Application_Start method is called only one time during the life cycle of an application.  
    

    See MSDN

    Therefore you are creating the one context for all requests. Then after sometimes it may be disposed. And it’s not a good practice to keep one DbContext for all the requests (memory issues etc..).
    So you can try putting that code in the Application_BeginRequest .

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

Sidebar

Related Questions

Folks, I've got an ASP.NET MVC application that I am attempting to secure using
I'm using the jQuery.Cascade plugin in my Asp.Net MVC application. I've got it all
I just got started running UI tests against my ASP.NET MVC application using WatiN.
I'm working on my first ASP.NET MVC 3 application and I've got two Controllers
When running my first asp.net mvc application I got this error I thought that
I'm working in an ASP.NET 3.5 MVC application where i've got a view with
I've got the following ASP.NET 4 MVC 3 code in a view: @Html.TextBoxFor(model =>
I've currently got a ASP.NET MVC 2 application on .NET 3.5 and I want
I've got a basic ASP.NET MVC 2 application. I've got adding and editing rows
I just got done reading Rick Anderson's article on Securing your ASP.NET MVC application

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.