这是我第一次使用Dapper.Contrib ( Nuget的最新版本),这是一个奇怪的情况:
using (SqlConnection cn = new SqlConnection(connectionString))
{
cn.Open();
var product = cn.Get<Product>(1);
}
在SqlMapperExtensions上,它引发错误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))
}
数据库select * from Products where Id = @id
接收命令select * from Products where Id = @id
是错误的。
s
产品? 我尝试过其他表并获得相同的结果。
看来是这样写的,可以检查源代码
或更具体地说:
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
{
...
}