Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
403 views
in Technique[技术] by (71.8m points)

c# - 在行'await _context.SaveChangesAsync();'中获取错误 在C#的POST API中(Getting error on line 'await _context.SaveChangesAsync ();' in POST api in c#)

Sharing below the details encountered while hitting a POST request.

(在遇到POST请求时,在下面共享遇到的详细信息。)

The code stops at the line

(代码停在一行)
await _context.SaveChangesAsync ();

ERROR :

(错误:)

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
pgsql-entityframeworkcore-postgresqlsrcEFCore.PGUpdateInternalNpgsqlModificationCommandBatch.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
pgsql-entityframeworkcore-postgresqlsrcEFCore.PGStorageInternalNpgsqlExecutionStrategy.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 Controller:

(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 });
        }

Method called :

(方法称为:)

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;
        }

Versions: Postgres 11.5

(版本:Postgres 11.5)
.NET 2.2.0

(.NET 2.2.0)

  ask by Lakshay Bansal translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...