public async Task ApplyPatchBasedOnDto <TEntity, TDto, TKey>(TKey id, TDto dto) where TEntity : class
        {
            if (dto == null)
            {
                throw new ArgumentNullException($"{nameof(dto)}", $"{nameof(dto)} cannot be null.");
            }

            if (id.IsNullOrEmpty())
            {
                throw new ArgumentNullException($"{nameof(id)}", $"{nameof(id)} cannot be null or empty.");
            }

            var properties = dto.GetFilledProperties();

            var updateExpressions = properties.Select(BuildExpressionByPropertyName <TEntity>).ToArray();

            //dto.Id = id;

            var entityToUpdate = _mapper.Map <TEntity>(dto);

            _context.Attach(entityToUpdate);

            foreach (var property in properties)
            {
                _context.Entry(entityToUpdate).Property(property).IsModified = true;
            }

            await _context.SaveChangesAsync();
        }
Esempio n. 2
0
        public void Update(TEntity entity)
        {
            if (_dbContext.Entry(entity).State == EntityState.Detached)
            {
                _dbContext.Attach(entity);
            }

            _dbContext.Set <TEntity>().Update(entity);
            _dbContext.SaveChanges();
        }
        protected void RemoveEntities(params object[] entities)
        {
            foreach (var entity in entities)
            {
                if (_context.Entry(entity).State == EntityState.Detached)
                {
                    _context.Attach(entity);
                }

                _context.Remove(entity);
            }
            _context.SaveChanges();
        }
 public void UpdateBookForAuthor(Book book)
 {
     _context.Attach(book);
     _context.Entry(book).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
 }