Dapper sqlmapperextensions会自动添加“ s”。到表名? [英] Dapper sqlmapperextensions automatically adds "s" to tablename?

查看:439
本文介绍了Dapper sqlmapperextensions会自动添加“ s”。到表名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我对 Dapper.Contrib (来自Nuget的最新版本)的第一次体验,这是一个奇怪的情况:

This is my first experience with Dapper.Contrib (latest version from Nuget) and it's a strange situation:

using (SqlConnection cn = new SqlConnection(connectionString))
{
    cn.Open();
    var product = cn.Get<Product>(1);
}

在SqlMapperExtensions上,它引发错误无效的对象名'产品'

On SqlMapperExtensions, it raises the error Invalid object name 'Products':

public static T Get<T>(this IDbConnection connection,
                       dynamic id, 
                       IDbTransaction transaction = null, 
                       int? commandTimeout = null) where T : class
{
    var type = typeof(T);
    string sql;
    if (!GetQueries.TryGetValue(type.TypeHandle, out sql))
}

数据库从Id = @id 的产品中接收命令 select *,这是错误的。

The database receives the command select * from Products where Id = @id which is wrong.

我已经尝试过使用其他表并获得相同的结果。

I've tried with other tables and get the same result.

推荐答案

似乎是这样写的,您可以检查一下 源代码

It seems that it's written this way, you can check the source code

或更具体地说:

private static string GetTableName(Type type)
{
    //.... codes

    if (TableNameMapper != null)
    {
        name = TableNameMapper(type);
    }
    else
    {
        var tableAttr = //.... lookup attribute
        if (tableAttr != null)
            name = tableAttr.Name;
        else
        {
            name = type.Name + "s";
            if (type.IsInterface() && name.StartsWith("I"))
                    name = name.Substring(1);
        }
    }

如果要使用文字类型名称,可以轻松配置它。

If you want to use the literal type name you can easily configure this.

SqlMapperExtensions.TableNameMapper = (type) => {
    //use exact name
    return type.Name;
};

这篇关于Dapper sqlmapperextensions会自动添加“ s”。到表名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆