dapper.contrib postres 42P01 错误:关系“<table name>”不存在

问题描述 投票:0回答:2

使用 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();
        }
    }
c# postgresql dapper dapper-contrib
2个回答
1
投票

你可以看到 Dapper 正在生成什么:

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

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

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

0
投票

如果数据库中的表名是类名的复数形式,可以用这个

SqlMapperExtensions.TableNameMapper = (type) => $"\"{Inflector.Inflector.Pluralize(type.Name)}\"";
© www.soinside.com 2019 - 2024. All rights reserved.