예제 #1
0
        /// <inheritdoc />
        public async Task <TEntity> UpdateAsync(TKey key, TEntity entity, CancellationToken cancellationToken)
        {
            entity.Id = key;

            var entry = _context.Entry(entity);

            if (entry.State == EntityState.Detached)
            {
                var set = GetDbSet();

                var found = await set.FindAsync(key);

                if (found != null)
                {
                    entity.CopyPropertiesTo(found, "Id");
                }
                else
                {
                    set.Attach(entity);
                    entry.State = EntityState.Modified;
                }
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(entry.Entity);
        }
 public async Task CerateFailureTypeAsync(FailureType type)
 {
     _dbContext.FailureTypeCategories.Attach(type.Category);
     _dbContext.Entry(type.Category).State = EntityState.Unchanged;
     _dbContext.FailureTypes.Add(type);
     await _dbContext.SaveChangesAsync();
 }
예제 #3
0
 public virtual void Delete(TEntity entityToDelete)
 {
     if (context.Entry(entityToDelete).State == EntityState.Detached)
     {
         dbSet.Attach(entityToDelete);
     }
     dbSet.Remove(entityToDelete);
 }
예제 #4
0
 public void Update(T entity)
 {
     if (unitOfWork.Entry(entity).State == EntityState.Detached)
     {
         unitOfWork.Set <T>().Attach(entity);
     }
     unitOfWork.Entry(entity).State = EntityState.Modified;
 }
예제 #5
0
 public void Remove(TEntity entityToDelete)
 {
     if (_dbContext.Entry(entityToDelete).State == EntityState.Detached)
     {
         _dbContext.Set <TEntity>().Attach(entityToDelete);
     }
     _dbContext.Set <TEntity>().Remove(entityToDelete);
 }
예제 #6
0
        /// <summary>
        /// Deletes provided entity in repository collection of entities.
        /// </summary>
        /// <param name="entity">Entity to be deleted.</param>
        public virtual void Delete(TEntity entity)
        {
            if (_dbContext.Entry(entity).State == EntityState.Detached)
            {
                _dbSet.Attach(entity);
            }

            _dbSet.Remove(entity);
        }
예제 #7
0
        /// <summary>
        /// Modify a item in a table. item never be Modified until call savechanges method.
        /// </summary>
        /// <param name="item">object of a class which will be Modified into corresponding DB table.</param>
        public virtual void Modify(T item)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }
            var entry = _context.Entry(item);

            DbSet.Attach(item);
            entry.State = EntityState.Modified;
        }
예제 #8
0
        public T Update(T entity)
        {
            _dbSet.Attach(entity);
            _appDbContext.Entry(entity).State = EntityState.Modified;

            return(entity);
        }
        public async Task SaveAsync(FailureReport report)
        {
            if (report.Feedback != null)
            {
                if (report.FeedbackId == null)
                {
                    _dbContext.Feedbacks.Add(report.Feedback);
                }
                else
                {
                    report.Feedback.Id = report.FeedbackId.Value;
                    _dbContext.Feedbacks.Attach(report.Feedback);
                    _dbContext.Entry(report.Feedback).State = EntityState.Modified;
                    await _dbContext.SaveChangesAsync();
                }
            }

            _dbContext.FailureReports.Attach(report);
            _dbContext.Entry(report).State = EntityState.Modified;

            await _dbContext.SaveChangesAsync();
        }
        public async Task SaveUserAsync(User user, IList <UserRole> newRoles)
        {
            _dbContext.Users.Attach(user);

            if (newRoles != null)
            {
                user.Roles.Clear();
                await _dbContext.SaveChangesAsync();

                newRoles.ToList().ForEach(r =>
                                          user.Roles.Add(_dbContext.UserRoles.First(role => role.Id == r.Id)));
            }

            _dbContext.Entry(user).State = EntityState.Modified;
            await _dbContext.SaveChangesAsync();
        }
예제 #11
0
 public void Delete(T entity)
 {
     context.Entry <T>(entity).State = EntityState.Deleted;
 }