Dapper sqlmapperextensions自动将“s”添加到tablename?

这是我第一次使用Dapper.ContribNuget的最新版本),这是一个奇怪的情况:

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

在SqlMapperExtensions上,它引发错误Invalid object name 'Products'

 public static T Get(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)) } 

数据库select * from Products where Id = @id接收命令select * from Products where Id = @id是错误的。

为什么它会附加到产品?

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

它似乎是用这种方式编写的,你可以查看源代码

或者更具体地说:

 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); } } 

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

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

它以这种方式设计。 您可以通过使用Dapper.Contrib.Extensions.TableAttribute修饰POCO类来覆盖默认行为。

 using Dapper.Contrib.Extensions; [Table("Product")] public class Product { ... } 

你的答案很棒,虽然如果你使用System.ComponentModel.DataAnnotations.Schema,那么让Dapper依赖于POCO Dapper的程序集并不是一个好习惯。 而不是使用Dapper.Contrib.Extensions