Dapper sqlmapperextensions automatically adds “s” to tablename?

后端 未结 2 2022
时光取名叫无心
时光取名叫无心 2021-01-04 10:53

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

using (SqlConnection cn = new Sql         


        
相关标签:
2条回答
  • 2021-01-04 11:19

    It is desgined this way. You can override the default behavior by decorating your POCO classes with Dapper.Contrib.Extensions.TableAttribute.

    using Dapper.Contrib.Extensions;
    
    [Table("Product")]
    public class Product
    {
    ...
    }
    
    0 讨论(0)
  • 2021-01-04 11:25

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

    Or more specifically:

    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;
    };
    
    0 讨论(0)
提交回复
热议问题