c# - '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
解决方案
推荐阅读
- r - 强制对 R 包进行串行编译
- node.js - 超级账本结构调用注册端点失败并出现错误
- c# - Admob 插页式 Unity 3D 问题
- javascript - 为什么我必须使用 window.onload 将图像绘制到画布上
- sql - 根据存储过程参数更改 SQL Server 查询
- vb.net - 在 MS Access 数据库中选择最大值
- excel - 为什么我的条件格式在工作表中工作时会失败?可能的间接问题?
- javascript - 如何通过 JSON 将表格数据从 HTML 前端传递到 Django 中的服务器端?
- java - 在java中通过SQL查询进行文本提取(分类)的最佳方法
- python - 在每一步从列表中切片大小为 N 的窗口