简体   繁体   English

ASP.Net Web API存储库更新错误500

[英]ASP.Net Web API Repository Update Error 500

My Web Api Controller has an UpdateArea method which is passed an id and the updated values in the body. 我的Web Api控制器有一个UpdateArea方法,该方法在主体中传递了id和更新的值。 It is written as follows: 内容如下:

[HttpPut("{id}")]
public async Task<IActionResult> UpdateArea(Guid id, [FromBody] AreaForUpdateDto areaForUpdateDto)
{
    // Check that the 'area' object parameter can be de-serialised to a AreaForUpdateDto.
    if (areaForUpdateDto == null)
    {
        _logger.LogError("AreasController.UpdateArea failed: Could not map area object parameter to a AreaForUpdateDto");

        // Return an error 400.
        return BadRequest();
    }

    // Ensure that the Area exists.
    var areaEntityFromRepo = _areaRepository.GetArea(id);

    // Map(source object (Dto), destination object (Entity))
    Mapper.Map(areaForUpdateDto, areaEntityFromRepo);
    _areaRepository.UpdateArea(areaEntityFromRepo);

    // Save the updated Area entity, added to the DbContext, to the SQL database.
    if (await _areaRepository.SaveChangesAsync())
    {
        var areaFromRepo = _areaRepository.GetArea(id);
        if (areaFromRepo == null)
            return NotFound();

        var area = Mapper.Map<AreaDto>(areaFromRepo);
        return Ok(area);
    }

    // The save has failed.
    _logger.LogWarning($"Updating Area {id} failed on save.");
    throw new Exception($"Updating Area {id} failed on save.");
}

This works fine until my application calls for an update, but the body of the request is identical to the existing data in the database. 在我的应用程序要求更新之前,这种方法可以正常工作,但是请求的主体与数据库中的现有数据相同。 When this happens the _areaRepository.SaveChangesAsync() fails returning an error 500 . 发生这种情况时, _areaRepository.SaveChangesAsync()无法返回error 500

This happens if the user opens an Edit Details dialog, does not change anything and then clicks the Edit button sending the request. 如果用户打开“编辑详细信息”对话框,不进行任何更改,然后单击发送请求的“编辑”按钮,则会发生这种情况。

I could validate on the client, which is fine, but I was surprised that this caused an error. 我可以在客户端上进行验证,这很好,但是令我惊讶的是,这导致了错误。

Can anybody please explain why this is failing and where I should correct this? 谁能解释为什么失败了,我应该在哪里纠正呢?

According to the logic explained by you 根据你解释的逻辑

This happens if the user opens an Edit Details dialog, does not change anything and then clicks the Edit button sending the request. 如果用户打开“编辑详细信息”对话框,不进行任何更改,然后单击发送请求的“编辑”按钮,则会发生这种情况。

and this snippet of code 和这段代码

//...code removed for brevity

// Save the updated Area entity, added to the DbContext, to the SQL database.
if (await _areaRepository.SaveChangesAsync())
{
    var areaFromRepo = _areaRepository.GetArea(id);
    if (areaFromRepo == null)
        return NotFound();

    var area = Mapper.Map<AreaDto>(areaFromRepo);
    return Ok(area);
}

// The save has failed.
_logger.LogWarning($"Updating Area {id} failed on save.");
throw new Exception($"Updating Area {id} failed on save.");

if no changes were made to the file and an update was attempted then no changes would be made to the DbContext which means that _areaRepository.SaveChangesAsync() would be false and thus continue on to 如果未对文件进行任何更改并尝试进行更新,则不会对DbContext ,这意味着_areaRepository.SaveChangesAsync()false ,因此继续进行操作

// The save has failed.
_logger.LogWarning($"Updating Area {id} failed on save.");
throw new Exception($"Updating Area {id} failed on save.");

and BAM 500 error on the server. 和服务器上的BAM 500错误。

I recommend debugging/stepping through the suspect code to confirm its behavior is as explained above and then rethink and refactor the update logic. 我建议调试/逐步执行可疑代码以确认其行为是否如上所述,然后重新考虑并重构更新逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM