Ejemplo n.º 1
0
        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();
        }
Ejemplo n.º 2
0
        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", "Offers"
                }.Contains(property.Name) && propValue == null)
                {
                    _db.Entry(currentUser).Property(property.Name).IsModified = false;
                }
            }

            _db.Entry(currentUser).Property(x => x.ConcurrencyStamp).IsModified = false;
            _db.Entry(currentUser).Collection(x => x.Offers).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);
        }