Home > Software design >  Update database changes on my tracked entity
Update database changes on my tracked entity

Time:01-28

I'm trying to update changes that happened in the database for a tracked entity and update my entity.

Follow the code

var user = await _userRepository.GetByUserId(userId);
_userRepository.Update(user); // _dbSet.Update(obj); 
_unitOfWork.Commit(); // _entityContext.SaveChanges();

await Task.Delay(delay);

_unitOfWork.AcceptAllChanges(); // _entityContext.ChangeTracker.AcceptAllChanges();

In that time that the application was waiting for the task.delay, another service made changes to the database for this same entity I am tracking, I try to fetch the changes from the database and update my entity with AcceptAllChanges but it doesn't work.

Does anyone know what could be wrong? Thank you in advance

CodePudding user response:

You want EntityEntry.Reload. Otherwise even if you re-query the entity, EF doesn't update the entity in memory.

db.Entry(user).Reload();
  • Related