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?