PostgresException:23505:重复的键值违反唯一约束“PK_country”

Nav*_*tor 7 c# entity-framework npgsql entity-framework-core asp.net-core-2.0

我正在使用 EF Core 2.0 和 Postgres 9.6。每当我插入数据时,我都会收到错误

PostgresException:23505:重复的键值违反唯一约束“PK_country”

通过跟踪它看起来像 EF 不生成 AutoIncrement 列

我的模特

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

我的控制器

if (ModelState.IsValid)
            {
                _context.Add(country);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
Run Code Online (Sandbox Code Playgroud)

例外

> 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()
Run Code Online (Sandbox Code Playgroud)

可能是什么问题呢?

Nav*_*tor 5

我找到了解决办法。问题是我使用 sql 文件中的脚本为数据库添加种子。

context.Database.ExecuteSqlCommand(File.ReadAllText(baseDir + "\\data.sql"));
Run Code Online (Sandbox Code Playgroud)

插入物包含

ALTER TABLE country DISABLE TRIGGER ALL;
INSERT INTO country .......
Run Code Online (Sandbox Code Playgroud)

所以序列当前值没有更新。当前值保持为 1。所以当我插入应用程序时。自动增量变为 1,因此PostgresException: 23505: 重复的键值违反了唯一约束 "PK_country"

解决

ALTER TABLE 国家 ENABLE TRIGGER ALL之后;添加SELECT pg_catalog.setval(pg_get_serial_sequence('country', 'country_id'), MAX(country_id)) FROM 国家;

现在它的应用程序成功运行。

希望能帮助别人。