c# - .NET Core API -> 更新对象也会更新以前加载的对象
问题描述
我已经实现了一个系统来记录所有控制器上的每个创建/更新/删除操作。基本上,这个想法很简单,因为当我在更新控制器中更新对象时,我首先执行获取然后更新,因此我可以访问原始对象和更新的对象,如下所示。
public async Task<IActionResult> Update([FromRoute] Guid groupId, [FromRoute] Guid licenseId, [FromBody] UpdateLicenseRequest request)
{
var cLicense = await _licenseService.GetAsync(groupId, licenseId);
if (cLicense == null)
return BadRequest(_localizer["LicenseNotExist"]);
var uLicense = await _licenseService.UpdateAsync(cLicense, _mapper.Map<License>(request));
if (uLicense == null)
return BadRequest();
await _auditService.CreateAsync(AuditTypeEnum.SVAULT_LICENSE_UPDATE, cLicense, uLicense);
return Ok(_mapper.Map<LicenseResponse>(uLicense));
}
以下是 GetAsync 方法的详细信息:
public async Task<License> GetAsync(Guid groupId, Guid licenseId)
{
RSUser cRSUser = await _cloudService.GetCloudAsync(groupId);
var rxLicense = await Task.Run(() =>
{
return cRSUser.getLicense(licenseId.ToString());
});
List<LicenseState> lstStates = await _context.LicenseStates.ToListAsync();
List<LicenseType> lstTypes = await _context.LicenseTypes.ToListAsync();
List<LicenseStatus> lstStatus = await _context.LicenseStatus.ToListAsync();
List<LicenseProduct> lstProducts = await _context.LicenseProducts.ToListAsync();
Dictionary<string, Group> dcGroups = _context.GroupProperties.AsNoTracking().Where(w => w.Name == GroupProperties.community_id.ToString() && (w.Value == rxLicense.P_community_id || w.Value == rxLicense.P_community_customer_id || w.Value == rxLicense.P_community_reseller_id)).Include(w => w.Group).ToDictionary(w => w.Value, w => w.Group);
if (rxLicense == null)
return null;
return License.ToObject(rxLicense, dcGroups, lstStates, lstTypes, lstStatus, lstProducts);
}
还可以找到 Update 方法的详细信息:
public async Task<License> UpdateAsync(License cLicense, License nLicense)
{
RSUser cRSUser = await _cloudService.GetCloudAsync(cLicense.Group.Id);
var result = await Task.Run(() =>
{
cLicense.TypeId = nLicense.TypeId;
cLicense.DataLimit = nLicense.DataLimit;
cLicense.Email = nLicense.Email;
cLicense.Notes = nLicense.Notes;
if (cLicense.TypeId == 8)
cLicense.DataLimit = 0;
return cRSUser.updateLicense(cLicense.ToRSUserClass(), new List<RX_licensemode>() { new RX_licensemode() { P_id = nLicense.ProductId.ToString().ToUpper() } }); ;
});
if (result != RSC_errorcode.success)
return null;
return await GetAsync(cLicense.Group.Id, cLicense.Id);
}
当我在 cLicense 上进行调试并停止时,我看到这个对象是当前的许可证(这是我想要的)但是一旦 UpdateAsync 方法运行,cLicense 也会更新,而我希望它保持为原始对象.
有没有人已经有同样的行为?我确定这是一个正常的过程,但我不知道如何让它按我的意愿工作。
随意询问是否有任何不清楚的地方。问候,
解决方案
When you pass cLicense
to UpdateAsync
, you aren't passing a copy, you are passing THE cLicence
. In UpdateAsync
you are setting the properties of cLicense
to the values of nLicense
, which is why you are seeing the values change.
推荐阅读
- c# - 代码.net核心的顺序应该是什么?
- c# - 如果 WPF 中已经有内容,我如何跳过第一个按钮?
- php - 所以......目标类不存在......好吗?
- javascript - 使用 render_template 将数据从 Flask 发送到 javascript
- html - 如何折叠所有打开的选择器或 html 元素
- mysql - Colesce 和内连接
- c++ - mktime() 在调用时重新分配堆内存
- flutter - 如何在 Catch Block 中使用 Flutter AlertDialog?扑
- scala - 如何在 (x)ubuntu 20.04 上安装独立的 Scala Spark
- python - Python/pandas:如何正确读取带有编码“cp1252”的西里尔文文本的 csv 文件?