This is my first experience with Dapper.Contrib (latest version from Nuget) and it's a strange situation:
这是我第一次与Dapper打交道。这是一个奇怪的情况:
using (SqlConnection cn = new SqlConnection(connectionString))
{
cn.Open();
var product = cn.Get<Product>(1);
}
On SqlMapperExtensions, it raises the error Invalid object name 'Products'
:
在SqlMapperExtensions中,它会引发错误无效的对象名称“产品”:
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))
}
The database receives the command select * from Products where Id = @id
which is wrong.
数据库接收来自Id = @id错误的产品的命令select *。
Why does it append s
to Product?
I've tried with other tables and get the same result.
我试过其他的表格,结果也一样。
3 个解决方案
#1
6
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;
};
#2
11
It is desgined this way. You can override the default behavior by decorating your POCO classes with Dapper.Contrib.Extensions.TableAttribute
.
它是通过这种方式进行的。您可以通过使用dapper. design . extension . tableattribute来装饰您的POCO类来覆盖默认行为。
using Dapper.Contrib.Extensions;
[Table("Product")]
public class Product
{
...
}
#3
-2
Your answer is great, although it is not a good practice to have Dapper dependency on an assembly of POCO Dapper is happy if you do using System.ComponentModel.DataAnnotations.Schema; instead of using Dapper.Contrib.Extensions
您的答案很好,尽管如果您使用System.ComponentModel.DataAnnotations.Schema,那么将Dapper依赖于POCO Dapper的集合不是一个好的做法。而不是使用Dapper.Contrib.Extensions
#1
6
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;
};
#2
11
It is desgined this way. You can override the default behavior by decorating your POCO classes with Dapper.Contrib.Extensions.TableAttribute
.
它是通过这种方式进行的。您可以通过使用dapper. design . extension . tableattribute来装饰您的POCO类来覆盖默认行为。
using Dapper.Contrib.Extensions;
[Table("Product")]
public class Product
{
...
}
#3
-2
Your answer is great, although it is not a good practice to have Dapper dependency on an assembly of POCO Dapper is happy if you do using System.ComponentModel.DataAnnotations.Schema; instead of using Dapper.Contrib.Extensions
您的答案很好,尽管如果您使用System.ComponentModel.DataAnnotations.Schema,那么将Dapper依赖于POCO Dapper的集合不是一个好的做法。而不是使用Dapper.Contrib.Extensions