首页 > 解决方案 > .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 也会更新,而我希望它保持为原始对象.

有没有人已经有同样的行为?我确定这是一个正常的过程,但我不知道如何让它按我的意愿工作。

随意询问是否有任何不清楚的地方。问候,

标签: c#api.net-core

解决方案


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.


推荐阅读