首页 > 解决方案 > 'await _context.SaveChangesAsync ();'线上出现错误 在 C# 中的 POST api 中

问题描述

在下面分享点击 POST 请求时遇到的详细信息。代码停在该行
await _context.SaveChangesAsync ();

错误 :

fail: Microsoft.EntityFrameworkCore.Update[10000]
      An exception occurred in the database while saving changes for context type 'BuyAndSellApi.Models.Entities.BuyAndSellContext'.
      Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
      Parameter name: index
         at System.Collections.Generic.List`1.get_Item(Int32 index)
         at Npgsql.EntityFrameworkCore.PostgreSQL.Update.Internal.NpgsqlModificationCommandBatch.ConsumeAsync(RelationalDataReader reader, CancellationToken cancellationToken) in C:\projects\npgsql-entityframeworkcore-postgresql\src\EFCore.PG\Update\Internal\NpgsqlModificationCommandBatch.cs:line 193
         at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
         --- End of inner exception stack trace ---
         at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(DbContext _, ValueTuple`2 parameters, CancellationToken cancellationToken)
         at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken) in C:\projects\npgsql-entityframeworkcore-postgresql\src\EFCore.PG\Storage\Internal\NpgsqlExecutionStrategy.cs:line 49
         at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IReadOnlyList`1 entriesToSave, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)

POST 控制器:

[HttpPost("register")]
        public async Task<IActionResult> Register(UserDto userDto)
        {
            if (await _repo.UserExists(userDto.UserName))
                return BadRequest("Username already exists");

            var userToCreate = _mapper.Map<User>(userDto);
            var createdUser = await _repo.Register(userToCreate, userDto.Password);
            return StatusCode(201, new { username = createdUser.UserName, fullname = createdUser.FirstName + " " + createdUser.LastName });
        }

方法调用:

public async Task<User> Register (User user, string password) {
            byte[] passwordHash, salt;
            CreatePasswordHash (password, out passwordHash, out salt);
            user.Password = passwordHash;
            user.Salt = salt;
            await _context.User.AddAsync (user);
            await _context.SaveChangesAsync ();  
            return user;
        }

版本:Postgres 11.5
.NET 2.2.0

标签: c#asp.net.netasp.net-mvcpostgresql

解决方案


推荐阅读