Entity Framework 不会抛出错误,但我在调试模式下看到它。

huangapple go评论64阅读模式
英文:

Entity Framework does not throw an error, but I see it in debug mode

问题

The code you provided appears to have an issue with LINQ translation related to the use of StringComparison.OrdinalIgnoreCase in the LINQ expression. This issue results in an error, but it doesn't throw an exception in the CreateOrUpdateUserSettingAsync() method. The error message suggests that the LINQ query cannot be translated due to the use of StringComparison with Equals.

控制器(Controller)中的代码似乎存在LINQ翻译的问题,与LINQ表达式中的StringComparison.OrdinalIgnoreCase有关。这个问题导致了一个错误,但它不会在CreateOrUpdateUserSettingAsync()方法中抛出异常。错误消息建议由于在Equals中使用StringComparison,无法翻译LINQ查询。

英文:

I have the following code which incorrectly includes .Equals(userSetting.UserId, StringComparison.OrdinalIgnoreCase)).

My question is why the code doesn't throw an exception. The CreateOrUpdateUserSettingAsync() method exits in the line

UserSetting? user = query.FirstOrDefault();

However, the try in the controller does catch an error. Why not?

Controller:

[HttpPut]
[Route("user")]
public ActionResult UpdateUser([FromBody] UserSetting userSetting)
{
    if (userSetting == null) return BadRequest("Invalid user Setting");

    try
    {
        this._myUserRepository.CreateOrUpdateUserSettingAsync(userSetting);

        return StatusCode(((int)HttpStatusCode.NoContent));
    }
    catch (Exception e)
    {
        return StatusCode((int)HttpStatusCode.InternalServerError, e);
    }
}

Repository:

Entity Framework 不会抛出错误,但我在调试模式下看到它。

Error:

> The LINQ expression 'DbSet<UserSetting>()
> .Where(u => u.UserId.Equals(value: __userSetting_UserId_0, comparisonType: OrdinalIgnoreCase))' could not be translated.
>
> Additional information: Translation of the 'string.Equals' overload with a 'StringComparison' parameter is not supported. See https://go.microsoft.com/fwlink/?linkid=2129535 for more information. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

答案1

得分: 4

你应该使用await来等待你的async方法调用:

public async Task<ActionResult> UpdateUser([FromBody] UserSetting userSetting)
{
    // ...
    try
    {
        await this._myUserRepository.CreateOrUpdateUserSettingAsync(userSetting);

        // ...
    }
    catch (Exception e)
    {
        // ...
    }
}

否则,这基本上是一个“发后即忘”的任务,可能会导致多种问题(例如未观察到的异常,或者像DbContext这样的资源的“过早”释放)。

英文:

You should await your async method invocation:

public async ActionResult UpdateUser([FromBody] UserSetting userSetting)
{
    // ...
    try
    {
        await this._myUserRepository.CreateOrUpdateUserSettingAsync(userSetting);

        // ...
    }
    catch (Exception e)
    {
        // ...
    }
}

Otherwise this is basically a fire-and-forget task which can lead to multiple problems (like unobserved exceptions, or "premature" disposal of resources like DbContext)

huangapple
  • 本文由 发表于 2023年5月23日 00:46:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定