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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:12:40+00:00 2026-05-25T11:12:40+00:00

I created a web application using ASP.Net MVC 3 and EF 4.1, and I

  • 0

I created a web application using ASP.Net MVC 3 and EF 4.1, and I am using the UnitOfWork pattern, but nothing is getting committed to the database. All this is quite new to me, and I don’t know where to start to resolve this issue.

I based myself on this post to create my web application:

http://weblogs.asp.net/shijuvarghese/archive/2011/01/06/developing-web-apps-using-asp-net-mvc-3-razor-and-ef-code-first-part-1.aspx

The final code, which can be obtained here also has a service layer and the UnitOfWOrk is being injected into the services.

Instead of using the custom injector based on Unity 2 as they are in that project, I am using Unity.Mvc3.

Here is my IUnitOfWork class:

public interface IUnitOfWork
{
    void Commit();
}

And here is my UnitOfWork class:

public class UnitOfWork : IUnitOfWork
{
    private readonly IDatabaseFactory databaseFactory;
    private MyProjectContext dataContext;

    public UnitOfWork(IDatabaseFactory databaseFactory)
    {
        this.databaseFactory = databaseFactory;
    }

    protected MyProjectContext DataContext
    {
        get { return dataContext ?? (dataContext = databaseFactory.Get()); }
    }

    public void Commit()
    {
        DataContext.Commit();
    }
}

And here is how one of my service class look like:

public class RegionService : IRegionService
{
    private readonly IRegionRepository regionRepository;
    private readonly IUnitOfWork unitOfWork;
    public RegionService(IRegionRepository regionRepository, IUnitOfWork unitOfWork)
    {
        this.regionRepository = regionRepository;
        this.unitOfWork = unitOfWork;
    }  
    ...
}

At start-up, my UnitOfWork component is being registered like this:

container.RegisterType<IUnitOfWork, UnitOfWork>();

Now, no matter whether I try to insert, update or delete, no request is being sent to the database. What am my missing here?

UPDATE:

Here is the content of DataContext.Commit():

public class MyProjectContext : DbContext
{
     public DbSet<Region> Regions { get; set; }

    public virtual void Commit()
    {
        base.SaveChanges();
    }
}

And here is databaseFactory.Get():

public interface IDatabaseFactory : IDisposable
{
    MyProjectContext Get();
}

UPDATE #2:

Using the debugger, I am noticing that my Region service and controller constructors are getting called once when performing only a select, but they are called twice when performing an update. Is this normal?

  • 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-25T11:12:40+00:00Added an answer on May 25, 2026 at 11:12 am

    Ok, I found the culprit. It has to do with how I was registering my database factory.

    Instead of

    container.RegisterType<IDatabaseFactory, DatabaseFactory>();
    

    I needed

    container.RegisterType<IDatabaseFactory, DatabaseFactory>(new HierarchicalLifetimeManager());
    

    I found the information on this web site:

    http://www.devtrends.co.uk/blog/introducing-the-unity.mvc3-nuget-package-to-reconcile-mvc3-unity-and-idisposable

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

Sidebar

Related Questions

using VS 2008 i created a clean asp.net MVC application project and changed nothing
I have created a sample project using ASP.NET MVC 3 Web Application (Razor) template.
I need to create a web application using ASP.NET MVC, jQuery and web standards
We have created a web application, using ASP.NET, that allows users to upload documents
I have a pretty big web application that I created last year using ASP.NET
I have created an asp.net MVC application in VS 2010, and am using TFS.
I created one asp.net mvc application using linq to sql, and in the generated
I'm building a small web application with ASP.NET MVC 2, using db4o as a
Good Morning All, I am developing a new ASP.net MVC web application. Part of
I am creating a Web application using ASP.NET MVC, and I'm trying to use

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.