使用 dapper.contrib,我不断收到错误:'42P01: relation "tblproduct" does not exist'.

我相信这是因为 postgresql 区分大小写。实体本身具有 的模式注释'[Table("tblProduct")]'

我找不到为什么生成的 sql 会使用小写的,c#,postgresql,dapper,dapper-contrib"/>

首页 > 解决方案 > dapper.contrib postres 42P01 错误:关系“”不存在

使用 dapper.contrib,我不断收到错误:'42P01: relation "tblproduct" does not exist'.

我相信这是因为 postgresql 区分大小写。实体本身具有 的模式注释'[Table("tblProduct")]'

我找不到为什么生成的 sql 会使用小写的

问题描述

使用 dapper.contrib,我不断收到错误:'42P01: relation "tblproduct" does not exist'.

我相信这是因为 postgresql 区分大小写。实体本身具有 的模式注释'[Table("tblProduct")]'

我找不到为什么生成的 sql 会使用小写的表名?我正在使用'SqlMapperExtensions.TableNameMapper'来强制案例,但这也不起作用。我错过了什么吗?谢谢

    public ICollection<Product> GetAll(int count)
    {
        if (SqlMapperExtensions.TableNameMapper != null)
            return null;

        SqlMapperExtensions.TableNameMapper = (type) =>
        {
            return "tblProduct";
        };

        using (var connection = new NpgsqlConnection(connectionString))
        {
            connection.Open();
            return connection.GetAll<Product>().Take(count).ToList();
        }
    }

您可以尝试以下几行:

PGPASSWORD=password;

pg_restore --host "hostname" --port "port" --username "postgres" --no-password --verbose --dbname "dbname"  --schema "schema" "dump_filename";

其中“hostname”是 postgresql 服务器的主机名(localhost),“port”是监听 postgresql 服务器的端口(5432),“postgres”是用户名,“dbname”是数据库名称

标签: c#postgresqldapperdapper-contrib

解决方案


您可以看到 Dapper 生成的内容:

NpgsqlLogManager.Provider = new ConsoleLoggingProvider(NpgsqlLogLevel.Trace, true, true);

我用 qutoes 解决了我在 PostgresSQL 上的问题:

SqlMapperExtensions.TableNameMapper = (type) => $"\"{type.Name}s\"";

推荐阅读