Sto usando EF Core 2.0 e Postgres 9.6. Ogni volta che inserisco i dati ottengo l'errore
PostgresException: 23505: il valore chiave duplicato viola il vincolo univoco "PK_country"
Tracciando sembra che EF non generi la colonna AutoIncrement
Il mio modello
public partial class Country
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CountryId { get; set; }
[Display(Name="Country Name")]
public string CountryName { get; set; }
}
Il mio controller
if (ModelState.IsValid)
{
_context.Add(country);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
Eccezione
> 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()
Quale potrebbe essere il problema?
Anche se la soluzione qui è già stata trovata, la domanda non specifica che si caricano i dati da alcuni file .sql
. Se avessi caricato i dati nel modo seguente, anche questo avrebbe generato lo stesso errore:
_context.Add(new Country { CountryId = 0, CountryName = "MyCountry" });
_context.Add(new Country { CountryId = 1, CountryName = "MyCountry" });
Il motivo è che (non so perché) la prima riga avrà CountryId = 1
e la seconda anche CountryId = 1
. Il che porta a un valore chiave duplicato.
Questo succede con: