我试图想出实现“更改验证器”。我在后端使用实体框架,在前端使用角度。通过 WebApi 进行通信。出于多种原因,客户将我List<DepartmentSettingDto>
和其他属性发送给我的控制器。该列表可以包含许多用户无法修改的对象。DepartmentSetting
所以,如果数据库中的数据没有变化,我不想更新数据库。我想要这样的东西:
public void UpdateDepartmentsSettings(DepatmentsSettingsDto depatmentsSettings)
{
using (var transaction = new TransactionScope(TransactionScopeOption.RequiresNew
, new TransactionOptions {IsolationLevel = IsolationLevel.Serializable}))
{
foreach (var departmentSetting in DepatmentsSettingsDto.DepartmentSettings)
{
var currentSetting = _context.DepartmentSettings.Find(departmentSetting.Id)
if departmentSetting.Modified(currentSetting)
{
var newSetting = Mapper.Map<DepartmentSettingDto, DepartmentSetting>
(departmentSetting, currentSetting);
Mapper.Map(currentSetting, newSetting);
}
}
_context.SaveChanges();
transaction.Complete();
}
}
是否可以编写扩展方法或类似的方法来避免保存未修改的更改?提前致谢!