asp.net核心api错误数据库操作预期会影响1行,但实际上影响0行

时间:2019-09-16 17:10:47

标签: asp.net-core asp.net-core-webapi

.Net核心api中出现错误

Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. 
See http://go.microsoft.com/fwlink/?LinkId=527962

我认为该错误是由于上述链接中提到的并发问题引起的。基本上不存在尝试捕获功能,它向客户端抛出了错误。我也认为错误是在这行代码上引起的

等待_userUow.RemoveToken(token); 为了方便修复,我添加了try and catch块以防止引发异常。该解决方案是否足够好,或者我需要以不同的方式处理它。

 public async Task LogoutAsync(string identityUserName, string refreshToken)
        {
            // remove token from database
            try
            {
                var user = await _userUow.GetUserByNameAsync(identityUserName);
                Token token;
                if (user != null && !string.IsNullOrEmpty(refreshToken))
                {
                    token = _userUow.GetRefreshToken((int)user.Id, refreshToken);

                    if (token != null)
                    {
                        await _userUow.RemoveToken(token);
                    }
                }
                await LogCurrentActivity(identityUserName, "logout", true, null, null);
                await _userUow.CurrentUserSignOutAsync();
            }
            catch (Exception ex)
            {

            }
        }




         public async Task RemoveToken(Token token)
        {
            //invalidate old token
            _securityContext.Remove(token);
            //persist
            await _securityContext.SaveChangesAsync();
        }

0 个答案:

没有答案