sql - 带有 EntityFramework Core 的 Postgres 中的原始 SQL

标签 sql postgresql entity-framework-core quoted-identifier

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

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

失败并返回错误码

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

最佳答案

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

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

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

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

关于sql - 带有 EntityFramework Core 的 Postgres 中的原始 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56212851/

相关文章:

java - Java.sql 的 getRow() 线程安全吗?

sql - ORACLE SQL 如何使用自定义函数iside insert cte

php - SQL语法错误,PHP MYSQL

postgresql - 如何使用 SQLAlchemy 将列默认设置为 PostgreSQL 函数?

c# - EF核心: No backing field could be found for property of entity type and the property does not have a getter

c# - 使用 appsettings.json 配置 DbContext 映射

sql - 将结果行强制转换为对象

java - PostgreSQL 中的可修改字符串数组

sql - 在存储过程后面抽象我们的数据库有哪些优点和缺点?

asp.net-core-mvc - 使用 Entity Framework Core 缓存表