我知道当 Postgres 抛出重复键异常时,它通常会在异常本身的详细信息中给出重复元素的值。
ERROR: duplicate key value violates unique constraint "name_of_pkey"
DETAIL: Key (field_name)=(value) already exists.
在 C# 中,既然字段 Details 已从异常类中删除,我如何从 NpgSqlException (NpgSql 3.1.7.0) 中获取该值?
最佳答案
如 3.1 migration notes 中所述,在 Npgsql 3.1 中,PostgreSQL 错误被作为 PostgresException 抛出,它扩展了 NpgsqlException。 NpgsqlExceptin 会因各种客户端错误(例如网络)而被抛出。
您会在 PostgresException 上找到 Detail 属性。
关于C# 从 NpgSqlException 中捕获重复键的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40439225/