带有 EntityFramework 核心的 Postgres 中的原始 SQL

Raw SQL in Postgres with EntityFramework Core

假设我有一个客户 table 并且想使用原始 SQL 进行查询。以下代码不起作用:

List<Customer> customers = _db.Customer.FromSql("SELECT * FROM Customer").ToList();

失败,错误代码

'42P01: relation "customer" does not exist'

在某些情况下,Postgres 会生成区分大小写的 table 名称,因此您可能必须以这种方式引用 table 名称。这是通过在 table 名称周围添加引号来完成的,如下所示:"Customer".

您可能还需要包含架构。这应该有效:

List<Customer> customers = _db.Customer.FromSql("SELECT * FROM \"public\".\"Customer\"").ToList();

假设您的架构名称是“public”。否则插入您的架构名称。