using System.Linq.Expressions;

//
用表达式树,部分字段 Expression<Func<CourseSchedule, object>>[] updatedProperties = { p => p.createtime,
             p => p.teacher };

调用Helper类

  _courseScheduleRepository.Value.UpdateEntity(schedule, updatedProperties, true);

 

Helper类

        /// <summary>
        /// 更新部分字段
        /// </summary>
        public virtual int UpdateEntity(T entity, Expression<Func<T, object>>[] updatedProperties, bool IsCommit = true)
        {
            int result = 0;
            _dbContext.Set<T>().Attach(entity);
            if (updatedProperties.Any())
            {
                foreach (var property in updatedProperties)
                {
                    _dbContext.Entry<T>(entity).Property(property).IsModified = true;
                }
            }
            if (IsCommit)
            {
                result = _UnitOfWork.Commit();
            }

            return result;
        }

 

相关文章:

  • 2021-11-20
  • 2021-11-20
  • 2022-12-23
猜你喜欢
  • 2021-09-03
  • 2021-10-16
  • 2022-12-23
  • 2021-06-13
  • 2022-12-23
  • 2021-10-03
  • 2021-12-08
相关资源
相似解决方案