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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:44:53+00:00 2026-06-17T06:44:53+00:00

I am facing a problem to save data using UnitOfWork. I mean I am

  • 0

I am facing a problem to save data using UnitOfWork. I mean I am unable to save data using UnitOFWork.Commit() from Controller class. My Implementation check bellow.

IUnitOfWork

public interface IUnitOfWork<C> :  IDisposable
{
        int Commit();
        C GetContext { get; set; }
        TransactionScope BeginTransaction();
} 

UnitOfWork

   public class UnitOfWork<C> : IUnitOfWork<C> where C : DbContext
    {
        private bool _disposed;
        private readonly C _dbContext = null;
        private TransactionScope _transaction;

        public UnitOfWork()
        {
            GetContext = _dbContext ?? Activator.CreateInstance<C>();
        }

        public int Commit()
        {
            return GetContext.SaveChanges();
        }


        public C GetContext
        {
            get;
            set;
        }


        public TransactionScope BeginTransaction()
        {
            if (null != _transaction)
            {
                _transaction = new TransactionScope();
            }

            return _transaction;
        }

        #region IDisposable Members
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    if (null != _transaction)
                    {
                        _transaction.Dispose();
                    }

                    if (null != _dbContext)
                    {
                        _dbContext.Dispose();
                    }

                }

            }

            _disposed = true;
        }

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


    }

Now within RepositoryBase / GenericRepository

public abstract class RepositoryBase<C, E> : IRepository<E> where E : class where C : DbContext
    {
        private readonly IDbSet<E> _dbSet;

        protected RepositoryBase(IUnitOfWork<C> unitOfWork)
        {
            UnitOfWork = unitOfWork;

            _dbSet = UnitOfWork.GetContext.Set<E>();

        }

        protected IUnitOfWork<C> UnitOfWork
        {
            get;
            private set;
        }


        #region IRepository<E> Members

        public void Insert(E entity)
        {
            _dbSet.Add(entity);
            UnitOfWork.GetContext.Entry(entity).State = System.Data.EntityState.Added;
            UnitOfWork.Commit();
        }

[...]

When I use UnitOfWork.Commit(); within GenericRepository I am able to save data successfully. But When I use UnitOfWork.Commit(); withing Controller I am unable to save data.

Controller Code

    private readonly IEmployeeRepository _employeeRepository;

    public EmployeeController(IEmployeeRepository employeeRepositoty, IUnitOfWork<MyDbContext> unitOfWork)
    {
        UnitOfWork = unitOfWork;
        this._employeeRepository = employeeRepositoty;
    }

  [HttpPost]
        public ActionResult Create(EmployeeModel employeemodel)
        {
            if (ModelState.IsValid)
            {

                    using (UnitOfWork)
                    {
                        _employeeRepository.Insert(employeemodel);

                        UnitOfWork.Commit();  //OR UnitOfWork.GetContext.SaveChanges();                  

                    }  

                return RedirectToAction("Index");
            }

            return View(employeemodel);
        }

If I use UnitOfWork.commit() within Controller then GenericRepository Insert method code check bellow

        public void Insert(E entity)
        {
            _dbSet.Add(entity);
            UnitOfWork.GetContext.Entry(entity).State = System.Data.EntityState.Added;
        }

As per my understanding this is a designing issue of UnitOfWork. Please help me to solve that.

IEmployeeRepository

public interface IEmployeeRepository : IRepository<EmployeeModel>
{

}

public class EmployeeRepository : RepositoryBase<MyDbContext, EmployeeModel>, IEmployeeRepository
{
    public EmployeeRepository(IUnitOfWork<MyDbMContext> unitOfWork)
        : base(unitOfWork)
    {

    }
}

NinjetDIConfiguration

kernel.Bind<IUnitOfWork<MyDbContext>>().To<UnitOfWork<MyDbContext>>();
kernel.Bind<ICountryRepository>().To<CountryRepository>();
kernel.Bind<IUserProfileRepository>().To<UserProfileRepository>();
kernel.Bind<IEmployeeRepository>().To<EmployeeRepository>();
  • 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-17T06:44:54+00:00Added an answer on June 17, 2026 at 6:44 am

    Probably Wrong DI (Ninject) Configuration create the problem. Solution check bellow.

    kernel.Bind<IUnitOfWork<MyDbContext>>().To<UnitOfWork<MyDbContext>>().InRequestScope();
    kernel.Bind<IEmployeeRepository>().To<EmployeeRepository>().InRequestScope();
    

    Actualy I don’t use .InRequestScope(); in my previous configuration. that’s why It create mysterious behavior.

    But Still unable to understand the reason behind that. Clarification welcome.

    According the clarification of Mystere Man (Problem: Since you use UnitOfWork both in your repository, and in your controller, you had two different instances, and each instance had its own context. So, you added the entities to one context (the one in your repository) but you called SaveChanges on a different context. Since that different context didn’t have anything added to it, nothing happened.

    Suggestion: you shouldn’t need to make the repository InRequestScope, only the UnitOfWork, since that holds the context).

    So the Best solution check bellow.

    kernel.Bind<IUnitOfWork<MyDbContext>>().To<UnitOfWork<MyDbContext>>().InRequestScope();
    kernel.Bind<IEmployeeRepository>().To<EmployeeRepository>();
    

    Without using _dbContext ?? Activator.CreateInstance<C>(); , can it be possible to get DbContext instance via Ninject ?

    Yes it is poositble According to MystereMan Suggestion. Check the solution bellow

    Ninject DI Configuration

    kernel.Bind<MyDbContext>().ToSelf().InRequestScope();
    kernel.Bind<IUnitOfWork<MyDbContext>>().To<UnitOfWork<MyDbContext>>();
    kernel.Bind<IEmployeeRepository>().To<EmployeeRepository>();
    

    And within UnitOfWork

       public class UnitOfWork<C> : IUnitOfWork<C> where C : DbContext
        {
            private readonly C _dbcontext;
    
            public UnitOfWork(C dbcontext)
            {
                _dbcontext = dbcontext;
            }
    
            public int Commit()
            {
               return _dbcontext.SaveChanges();
            }
    
            public C GetContext
            {
                get
                {
                    return _dbcontext;
                }
    
            }
    [...]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am facing a problem getting Entity Framework to save results from yield return
I'm facing a problem when saving an UIImage, instanciated from data received from an
I'm facing problem in retrieving data and images from the remote database server. I
iam facing problem in passing array to view. this is my controller code.. function
I am facing problem to get variable from query string. I have used htaccess
I am facing problem with UIPanGestureRecognizer. suppose i am adding 10 button dynamicaly using
Currently I am facing the tedious problem of exporting complete GridView data to the
Well i am not using email verification so i am facing problem in checking
i am facing one problem. i want to save settings in app.config file i
I am facing a really strange problem with Core Data. Let's describe it: Definitions

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.