简体   繁体   English

抛出异常:System.Private.CoreLib.ni.dll中的'Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException'

[英]Exception thrown: 'Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException' in System.Private.CoreLib.ni.dll

I created this package assign method: 我创建了这个包分配方法:

 [HttpPut("api/auth/user/{id}")]
    public async Task<IActionResult> AssignPackage(string id ,[FromBody] AppUser user)
    {
        try
        {
            var newUser = Mapper.Map<AppUser, UserViewModel>(user);

            var userToEdit = await _context.AppUser
            .AsNoTracking()
            .SingleOrDefaultAsync(m => m.Id == id);

            newUser.PackageType = user.AccountType;

            if (userToEdit == null)
            {
                return NotFound("Could not update user as it was not found");
            }
            else
            {
                _context.Update(user);
                await _context.SaveChangesAsync();
                //return Ok(newUser);
            }


            UserViewModel _userVM =

              Mapper.Map<AppUser, UserViewModel>(user);

            return new OkObjectResult(_userVM);
        }
        catch (DbUpdateException)
        {
            //Log the error (uncomment ex variable name and write a log.)
            ModelState.AddModelError("", "Unable to save changes. " +
                "Try again, and if the problem persists, " +
                "see your system administrator.");
            return NotFound("User not Found");
        }


    }

The aim of the method is that I input the Account type in postman and the output should be the user view model with an updated Package Type. 该方法的目的是我在邮递员中输入Account类型,输出应为具有更新Package类型的用户视图模型。 However when inputting the accountType into postman the output is 'User not found' and the error is '404 Not found' . 但是,将accountType输入postman ,输出为'User not found'和错误为'404 Not found'

In Visual Studio the error being shown is the title of the question. Visual Studio中,显示的错误是问题的标题。 unsure what the problem may be (only beginner level experience). 不确定可能是什么问题(仅限初学者水平的经验)。

The models being used: 使用的模型:

 public class AppUser : IdentityUser 
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string AccountType { get; set; }
        public int? PeriodOrderQty { get; set; }
        public int? TotalOrderQty { get; set; }
        public Guid? APIKey { get; set; }
        public Packages Package { get; set; }
        public Cities City { get; set; }
        public QualificationLevels Qualifications { get; set; }
        public string Token { get; set; }

    }

public class UserViewModel
    {
        public string Id { get; set; }
        public string UserName { get; set; }
        public string Email { get; set; }
        public string PackageType { get; set; }
        public int? PeriodOrderQty { get; set; }
        public int? TotalOrderQty { get; set; }
        public Guid? APIKey { get; set; }
    }

Given that AppUser inherits from IdentityUser it seems that when you call _context.Update(user); 鉴于AppUserIdentityUser继承,似乎当您调用_context.Update(user); there is no Id . 没有Id

I suspect that if you check that field it will be 0, as it's coming in as part of the body. 我怀疑如果您检查该字段,该字段将为0,因为它将作为正文的一部分出现。

EF can't find an entity to update because of this, and that's why you're getting the exception 因此,EF找不到要更新的实体,所以这就是您遇到异常的原因

暂无
暂无

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

相关问题 Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException - Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException 如何查找System.Private.CoreLib.ni.dll中发生“ System.Runtime.InteropServices.SEHException”的原因? - How to find reason for 'System.Runtime.InteropServices.SEHException' occurred in System.Private.CoreLib.ni.dll? OracleModificationCommandBatch.Consume():Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException - OracleModificationCommandBatch.Consume() : Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException 抛出异常:System.Private.CoreLib.dll 中的“System.MissingMethodException” - Exception thrown: 'System.MissingMethodException' in System.Private.CoreLib.dll 抛出异常:Microsoft.VisualStudio.TestPlatform.Common.dll AND System.Private.CoreLib.dll 中的“System.IO.FileLoadException” - Exception thrown: 'System.IO.FileLoadException' in Microsoft.VisualStudio.TestPlatform.Common.dll AND System.Private.CoreLib.dll 实体框架:更新具有 IEnumerable 属性的实体时出错。 &#39;Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException&#39; - Entity Framework: Error updating an Entity with IEnumerable property. 'Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException' Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException:'数据库操作预计会影响 1 行,但实际上会影响 2 行 - Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: 'Database operation expected to affect 1 row(s) but actually affected 2 row(s) System.Private.CoreLib.ni中发生了“ System.MissingMethodException” - 'System.MissingMethodException' occurred in System.Private.CoreLib.ni 引发异常:mscorlib.ni.dll uwp中的“ System.UnauthorizedAccessException” - Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.ni.dll uwp 抛出异常:mscorlib.ni.dll 中的“System.ArgumentNullException” - Exception thrown: 'System.ArgumentNullException' in mscorlib.ni.dll
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM