EFcore 修改部分字段

吃可爱长大的小学妹 提交于 2020-08-07 03:10:17
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;
        }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!