0

Ich verwende EF Core 2.0 und Postgres 9.6. Immer, wenn ich Daten einfügen erhalte ich die FehlerPostgresException: 23505: doppelte Schlüsselwert verstößt gegen eindeutige Einschränkung "PK_country"

PostgresException: 23505: doppelte Schlüsselwert verletzt einzigartige Einschränkung "PK_country"

Durch Tracing es wie EF sieht AutoInciment Spalte

My Model tut zu erzeugen

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

My-Controller

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

Exception

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

Was könnte das Problem sein?

+0

Bitte aktualisieren Sie den Post und die Tags. ASP.NET CORE2 ist irrelevant, EF ** Core **, Version und verwendeter Postgres-Provider. –

+0

Ok Ich udated die Post – Navigator

+0

Danke. Siehe [Serial (Autoinkrement) -Spalten] (http://www.npgsql.org/efcore/value-generation.html#serial-autoincrement-columns) und wenn das in [Aktualisieren von 1.0.x] beschriebene Problem (http://www.npgsql.org/efcore/migration/1.1.html#upgrading-from-10x) gilt für Ihr Szenario. –

Antwort

1

Ich fand die Lösung. Das Problem war, dass ich die Datenbank mit dem Skript aus der SQL-Datei seed.

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

Der Einsatz enthält

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

So wird der aktuelle Wert Sequenz wurde nicht aktualisiert werden. Aktueller Wert blieb 1.So, als ich mit der Anwendung einfügte. Autoincrement wird 1, daher PostgresException: 23505: Doppelschlüsselwert verletzt die eindeutige Integritätsbedingung "PK_country".

Auflösung

Nach ALTER TABLE Land ENABLE TRIGGER ALL; hinzufügen SELECT pg_catalog.setval (pg_get_serial_sequence ('Land', 'country_id'), MAX (country_id)) FROM Land;

Jetzt läuft es erfolgreich.

Hoffen Sie, dass jemand anderes hilft.

Verwandte Themen