
public virtual int Update(T entity, params Expression<Func<T, object>>[] updatedProperties) { var dbEntityEntry = dataContext.Entry(entity); if (updatedProperties.Any()) { foreach (var property in updatedProperties) { dbEntityEntry.Property(property).IsModified = true; } } else { foreach (var property in dbEntityEntry.OriginalValues.PropertyNames) { var original = dbEntityEntry.OriginalValues.GetValue<object>(property); var current = dbEntityEntry.CurrentValues.GetValue<object>(property); if (original != null && !original.Equals(current)) { dbEntityEntry.Property(property).IsModified = true; } } } return dataContext.SaveChanges(); }调用就更简单了,如:
_userRepository.Update(user,x=>x.Enabled);其中的Enabled即为user对应实体类的一个属性。
版权声明:本作品系原创,版权归码友网所有,如未经许可,禁止任何形式转载,违者必究。
发表评论
登录用户才能发表评论, 请 登 录 或者 注册