我正在使用EF Core 2.0和Postgres 9.6。每当我插入数据时,我都会收到错误
PostgresException:23505:重复键值违反唯一 约束" PK_country"
通过跟踪,看起来EF不会生成AutoInciment列
我的模特
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 :(得分:1)
我找到了解决方案。问题是我使用sql文件中的脚本为数据库播种。
$result = number_format(($number*900) /$boundary, $precision) . $postfix;
插入包含
context.Database.ExecuteSqlCommand(File.ReadAllText(baseDir + "\\data.sql"));
因此序列当前值未更新。当前值保持为1.所以当我插入应用程序时。 Autoincrment变为1因此 PostgresException:23505:重复键值违反唯一约束“PK_country”。
解决方案
ALTER TABLE country ENABLE TRIGGER ALL; 添加 SELECT pg_catalog.setval(pg_get_serial_sequence('country','country_id'),MAX(country_id))FROM country;
现在它的应用程序已经成功运行。
希望帮助别人。
答案 1 :(得分:1)
即使已经找到了此处的解决方案,该问题也没有指定您从某个.sql
文件中加载数据。如果您已通过以下方式加载数据,则也会产生相同的错误:
_context.Add(new Country { CountryId = 0, CountryName = "MyCountry" });
_context.Add(new Country { CountryId = 1, CountryName = "MyCountry" });
原因是,(我不知道为什么)第一行将有CountryId = 1
,第二行也有CountryId = 1
。导致重复的键值。
这发生在: