0

私はEF Core 2.0とPostgres 9.6を使用しています。 23505:重複するキー値がEF doesntのはAutoInciment列PostgresException:23505:重複するキー値がユニーク制約 "PK_country"に違反しています

私のモデルを生成するように見えるトレースすることにより、独特の 制約 "PK_country"

に違反し、私はデータを挿入するたびに、私はエラー

PostgresExceptionを取得します

public partial class Country 
    { 
     [Key] 
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
     public int CountryId { get; set; } 
     [Display(Name="Country Name")] 
     public string CountryName { get; set; } 
    } 

マイコントローラー

if (ModelState.IsValid) 
      { 
       _context.Add(country); 
       await _context.SaveChangesAsync(); 
       return RedirectToAction(nameof(Index)); 
      } 

例外

> PostgresException: 23505: duplicate key value violates unique 
> constraint "PK_country" 
> 
>  Npgsql.NpgsqlConnector+<DoReadMessage>d__148.MoveNext() 
>  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
>  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task 
> task) 
>  System.Runtime.CompilerServices.TaskAwaiter.GetResult() 
>  System.Runtime.CompilerServices.ValueTaskAwaiter.GetResult() 
>  Npgsql.NpgsqlConnector+<ReadMessage>d__147.MoveNext() 
>  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
>  Npgsql.NpgsqlConnector+<ReadMessage>d__147.MoveNext() 
>  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
>  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task 
> task) 
>  System.Runtime.CompilerServices.TaskAwaiter.GetResult() 
>  System.Runtime.CompilerServices.ValueTaskAwaiter.GetResult() 
>  Npgsql.NpgsqlDataReader+<NextResult>d__32.MoveNext() 
>  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
>  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task 
> task) 
>  Npgsql.NpgsqlDataReader+<<NextResultAsync>b__31_0>d.MoveNext() 
>  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
>  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task 
> task) 
>  Npgsql.NpgsqlCommand+<Execute>d__71.MoveNext() 
>  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
>  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task 
> task) 
>  System.Runtime.CompilerServices.TaskAwaiter.GetResult() 
>  System.Runtime.CompilerServices.ValueTaskAwaiter.GetResult() 
>  Npgsql.NpgsqlCommand+<ExecuteDbDataReader>d__92.MoveNext() 
>  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
>  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task 
> task) 
>  System.Runtime.CompilerServices.TaskAwaiter.GetResult() 
>  System.Runtime.CompilerServices.ValueTaskAwaiter.GetResult() 
>  Npgsql.NpgsqlCommand+<>c__DisplayClass90_0+<<ExecuteDbDataReaderAsync>b__0>d.MoveNext() 
>  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
>  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task 
> task) 
>  System.Runtime.CompilerServices.TaskAwaiter.GetResult() 
>  Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand+<ExecuteAsync>d__17.MoveNext() 
>  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
>  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task 
> task) 
>  System.Runtime.CompilerServices.TaskAwaiter.GetResult() 
>  Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch+<ExecuteAsync>d__32.MoveNext() 

何が問題だろうか?

+0

投稿とタグを更新してください。 ASP.NET CORE2は無関係で、EF ** Core **、バージョン、使用されるPostgresプロバイダーです。 –

+0

私は投稿をudatedしました – Navigator

+0

ありがとう。 [Serial(Autoincrement)Columns](http://www.npgsql.org/efcore/value-generation.html#serial-autoincrement-columns)を参照してください。[1.0.xからのアップグレード](http: /www.npgsql.org/efcore/migration/1.1.html#upgrading-from-10x)がお客様のシナリオに適用されます。 –

答えて

1

解決策が見つかった。問題は、私はSQLファイルからスクリプトを使用してデータベースをシードしていたことです。

context.Database.ExecuteSqlCommand(File.ReadAllText(baseDir + "\\data.sql")); 

挿入が

ALTER TABLE country DISABLE TRIGGER ALL; 
INSERT INTO country ....... 

が含まれていますので、シーケンスの現在値を更新していませんでした。現在の値は1のままでした。 Autoincrmentは1になりますPostgresException:23505:重複するキー値が一意制約 "PK_country"に違反しています。

解像度

ALTER TABLEの国後ALL TRIGGERをENABLE; add SELECT pg_catalog.setval(pg_get_serial_sequence( 'country'、 'country_id')、MAX(country_id))FROM国。

今すぐアプリケーションを実行してください。

希望者を助ける。

関連する問題