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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:03:41+00:00 2026-06-01T12:03:41+00:00

I am writing an ASP.Net MVC 3 Web Application using Entity Framework 4.1. My

  • 0

I am writing an ASP.Net MVC 3 Web Application using Entity Framework 4.1. My Unit of Work class is the same as described in this excellent tutorial http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

However, instead of injecting my UoW class into my Controller, I do so in my Service Class, like so

public class ListService : IListService
{
    private IUnitOfWork _uow;

    public ListService(IUnitOfWork uow)
    {
        _uow = uow;
    }

    public IList<List> GetAllListTypes()
    {
        return _uow.Lists.Get().OrderBy(l => l.description).ToList();
    }
}

My Unit of Work class is like this

public class UnitOfWork : IUnitOfWork, IDisposable
{
    readonly LocumEntities _context = new LocumEntities();
    private GenericRepository<List> _lists = null;

    public IGenericRepository<List> Lists
    {
        get
        {
            if (_lists == null)
            {
                _lists = new GenericRepository<List>(_context);
            }
            return _lists;
        }
    }

    public void Commit()
    {
        _context.SaveChanges();
    }

    private bool disposed = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                _context.Dispose();
            }
        }
        this.disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

}

And this all works fine. However, you’ll notice I have methods in my UoW class to dispose of the DbContext. I wish to dispose of the UoW class after every business transaction like is done in the above mentioned tutorial, they do this by using the following code in their controller

protected override void Dispose(bool disposing)
{
        db.Dispose();
        base.Dispose(disposing);
}

I tried to amend this method and place it in my service class however it does not work, ie, the dispose method in the UoW class never gets called

protected override void Dispose(bool disposing)
{
        _uow.Dispose();
}

I would greatly appreciate if someone could point me in the correct direction with how to dispose of my UoW class.

Thank you.

  • 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-01T12:03:42+00:00Added an answer on June 1, 2026 at 12:03 pm

    Folks

    The answer to this is, as I am using Unity for my IoC, it can be setup to take care of disposing of my Unit of Work class.

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

Sidebar

Related Questions

I'm developing a web application using ASP.NET MVC 3 and Entity Framework Code First
I am writing web application using ASP.NET MVC + NHibernate + Postres stack. I
Introduction I'm writing a web application (C#/ASP.NET MVC 3, .NET Framework 4, MS SQL
I am developing a web application using asp.net mvc... I am listing out the
I'm writing a ASP.NET MVC 3 web site. This is my custom MembershipProvider (only
I'm writing a ASP.NET web application. It dosen't use Web Forms or MVC. It
I have a web application written in ASP .NET MVC 3. I'm using ACS
My team is writing a content-managed web-hosting application in ASP.Net MVC 2 with the
I'm developing a web app in asp.net mvc using jquery, the language of application
Hey everyone.. This is my first time writing an ASP.NET MVC web app, and

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.