Update changes using Repository pattern with no Update command

Mr Edge 221 Reputation points
2022-03-12T12:32:23.403+00:00

Hoping someone could get me back on track here. Im going through a tutorial to introduce the Repository pattern with Entity Framework 6. So far its all been working in my project.
One of the comments made by the author is there is no need to have an Update statement as you should only save to the database once, at the end of your process. I mention this as all tutorials on the net have an update method.

I also have a Unit Of Work class which is called and it has a method to commit all changes after querying data i.e.

 myContext.SaveChanges()

Which writes back to the database. I can confirm this works when adding new items and retrieves data as intended. I currently have some code which first gets data from the repository in a List, makes changes to certain rows in the list, then calls the above SaveChanges method call Save. The problem i have come across is that it doesnt write the data back to the database.

When debugging, i noticed the changes are successfully made to the rows (in the List) as i intend but those changes dont get saved back to the database. Heres some code to give an example but ive tried to keep it short by not including everything (Some Customer interfaces and Repository class missing, but hopefully should be clear as the pattern is the same) however can provide if some one needs.

Root Repository Interface

    public interface IRepository<T> where T : class
    {
        T Get(int id);
        IEnumerable<T> GetAll();
        Etc
    }

Unit of Work Interface

    public interface IUnitOfWork : IDisposable
    {
        ICustomerRepository Customers { get; }
        int Save();
    }

DbContext

    public class ManageContext : DbContext
    {
        public ManageContext() : base("ConnectionString")
        {
            this.Configuration.LazyLoadingEnabled = false;
        }

        public virtual DbSet<Customer> Customers { get; set; }
    }

Unit of Work class

public class UnitOfWork : IUnitOfWork
    {
        private readonly ManageContext _context;
        public UnitOfWork(ManageContext context)
        {
            _context = context;
            Customers = new CustomerRepository(_context);
        }

        public ICustomerRepository Customers{ get; }

        public int Save()
        {
            return _context.SaveChanges();
        }

        public void Dispose()
        {
            _context.Dispose(); 
        }
    }

I get the data as below on my MVC web page/Controller (Im using Dependency Injection)

        private readonly IUnitOfWork _unitOfWork;
        public MyPageController(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public List<Customer> GetAllCustomers()
        {
            return _unitOfWork.Customers.GetAll();
        }

Heres the "problematic" area

        public void ChangeList(Customer cust)
        {
            var cs = _unitOfWork.Customers.GetAll().Take(10);

            // The list is modified here Code Not Provided
            // and i can confirm the changes are
            // showing in debug mode in the list that is returned back

            // Save Changes
            _unitOfWork.Save();
        }

I believe i understand what is going on as i had a similar experience with a single record when a function was created by passing in a Customer object. In this instance i had to load the existing record from the database which resolved the issue.

On this occasion i would like to know how to update a modified list and write the changes back to the database? If possible i would also like to know how to use the above pattern so i can resolve this issue in a single place (again if possible and if this is the recommended approach)?

I went through the tutorial again and the author does confirm no need for an Update method but im not sure how its possible?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,973 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,097 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,456 Reputation points
    2022-03-12T16:20:16.753+00:00

    Not an answer but instead say that there is more bad things about a repository pattern than there are good things.

    See Is the repository pattern useful with Entity Framework Core?

    Take time to read through the above.

    One of my issues is the DbContext lives throughout the life time of the app while when an app is written correct EF Core works best disconnected. When performing CRUD you simply can indicate the object state or use a method specific to an operation.

    You can have consistency with Interfaces and drop the entire repository pattern.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.