EFcore 修改部分字段

using System.Linq.Expressions;

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

调用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;
        }

猜你喜欢

转载自www.cnblogs.com/puzi0315/p/12892101.html