public virtual async Task UpdateAsync(long id, Entity entity, long?adminId = null) { var oldEntity = await GetByIdAsync(id); if (oldEntity == null) { return; //throw new EntityNotFoundException($"{EntityName} not found with the id: {id}"); } entity.UpdatedDate = DateTime.UtcNow; entity.Id = id; Db.Entry(oldEntity).CurrentValues.SetValues(entity); Db.Entry(oldEntity).Property(x => x.CreatedDate).IsModified = false; UpdatePropertyIgnore(entity); await Db.SaveChangesAsync(); }
public async Task UpdateAsync(User user, string[] roles = null) { var currentUser = await GetByIdAsync(user.Id); _db.Entry(currentUser).CurrentValues.SetValues(user); var type = typeof(User); var properties = type.GetProperties(); foreach (var property in properties) { var propValue = property.GetValue(user, null); if (!new List <string> { "ConcurrencyStamp", "Password" }.Contains(property.Name) && propValue == null) { _db.Entry(currentUser).Property(property.Name).IsModified = false; } } _db.Entry(currentUser).Property(x => x.ConcurrencyStamp).IsModified = false; var result = await _userManager.UpdateAsync(currentUser); if (!result.Succeeded) { return; //throw new EntityCreationException(string.Join("\n", result.Errors)); } if (roles != null && roles.Any()) { await _userManager.RemoveFromRolesAsync(currentUser, await _userManager.GetRolesAsync(currentUser)); await _userManager.AddToRolesAsync(currentUser, roles); } await _userManager.UpdateNormalizedEmailAsync(currentUser); await _userManager.UpdateNormalizedUserNameAsync(currentUser); }