コード例 #1
0
        public async Task <T> Update(T entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException($"{nameof(Update)} entity must not be null");
            }

            try
            {
                var itemInDB = await _context.Set <T>().AsNoTracking().Where(item => item.Id == entity.Id).FirstOrDefaultAsync();

                if (itemInDB != null)
                {
                    _context.Update(entity);
                    await _context.SaveChangesAsync();
                }

                return(entity);
            }
            catch (Exception ex)
            {
                throw new Exception($"{nameof(entity)} could not be updated: {ex.Message}");
            }
        }