PostgresException:23505:重复的键值违反了唯一约束"PK

编程入门 行业动态 更新时间:2024-10-27 23:19:05
本文介绍了PostgresException:23505:重复的键值违反了唯一约束"PK_country".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用EF Core 2.0和Postgres 9.6.每当我插入数据时都会出现错误

I am using EF Core 2.0 and Postgres 9.6. Whenever I insert data I get the error

PostgresException:23505:重复的键值违反了唯一 约束"PK_country"

PostgresException: 23505: duplicate key value violates unique constraint "PK_country"

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

By tracing it looks like EF doesnt generate AutoIncrement column

我的模特

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

可能是什么问题?

推荐答案

即使已经找到了此处的解决方案,该问题也没有指定您从某些.sql文件中加载数据.如果您已通过以下方式加载数据,则也会产生相同的错误:

Even though the solution here was already found, the question does not specify that you load the data from some .sql file. If you had been loading the data in the following way, this would have also generated the same error:

_context.Add(new Country { CountryId = 0, CountryName = "MyCountry" }); _context.Add(new Country { CountryId = 1, CountryName = "MyCountry" });

原因是,(我不知道为什么)第一行将具有CountryId = 1,第二行也将具有CountryId = 1.导致重复的键值.

The reason is, that (I don't know why) the first row will have CountryId = 1 and the second one also CountryId = 1. Which leads to duplicate key value.

这发生在:

  • Npgsql.EntityFrameworkCore.PostgreSQL 2.1.2
  • Microsoft.EntityFrameworkCore.Design 2.2.3

更多推荐

PostgresException:23505:重复的键值违反了唯一约束"PK

本文发布于:2023-10-20 08:50:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1510500.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:键值   违反了   PostgresException   quot   PK

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!