EFcore 修改部分字段


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM