为什么我需要使用 Npgsql for C# 和 PostgreSQL 来引用列和表?

Why do I need to quote columns and tables using Npgsql for C# and PostgreSQL?

所以我从 SQL Server 2008 R2(在 Windows Server 2012)切换到 PostgreSQL 9.5(再次在 Windows)和我所有的现有 SQL 个语句以这种格式编写:

INSERT INTO Address (Address1, Address4, Address5, Postcode, IsoCountryCode) 
VALUES(@P0, @P1, @P2, @P3, @P4)

我使用 Ngpsql v3.1.3 作为我的 .NET 库来使用 C# 与 PostgreSQL 对话。执行相同的语句会导致无法找到 table 地址的错误。为了让它工作,我必须将所有列名称和 table 名称用双引号括起来,如下所示:

INSERT INTO \"Address\" (\"Address1\", \"Address4\", \"Address5\", \"Postcode\", \"IsoCountryCode\") 
VALUES(@P0, @P1, @P2, @P3, @P4)

我的应用程序中有 600 多个 SQL 查询,我不想遍历所有查询并到处注入引号。

Npgsql 文档示例不使用引号,我是不是遗漏了什么?看这里Npgsql Documentation Example

具体来说,他们的例子是这样的:

INSERT INTO data (some_field) VALUES ('Hello world')

所以我想我不必双引号。谁能赐教一下?

如评论中所述,这就是 PostgreSQL 的工作方式 - PostgreSQL 隐式地将未加引号的标识符转换为小写。没有设置 AFAIK 来更改此行为。 The docs on lexical structure 说明:

Quoting an identifier also makes it case-sensitive, whereas unquoted names are always folded to lower case. For example, the identifiers FOO, foo, and "foo" are considered the same by PostgreSQL, but "Foo" and "FOO" are different from these three and each other. (The folding of unquoted names to lower case in PostgreSQL is incompatible with the SQL standard, which says that unquoted names should be folded to upper case. Thus, foo should be equivalent to "FOO" not "foo" according to the standard. If you want to write portable applications you are advised to always quote a particular name or never quote it.)