C# 简洁的模糊扩展方法
我正在将Dapper作为ORM解决方案进行测试,但遇到了一些扩展方法的问题,如C# 简洁的模糊扩展方法,c#,dapper,C#,Dapper,我正在将Dapper作为ORM解决方案进行测试,但遇到了一些扩展方法的问题,如Execute或QueryMultiple: using (SQLiteConnection con = new SQLiteConnection(GetConnectionString())) { con.Open(); string sql = @" select * from Customer where Id = @id; select * from Addres
Execute
或QueryMultiple
:
using (SQLiteConnection con = new SQLiteConnection(GetConnectionString()))
{
con.Open();
string sql = @"
select * from Customer where Id = @id;
select * from Address where CustomerId = @id;";
// QueryMultiple extension ambiguous?
using (var multi = con.QueryMultiple(sql, new { id = 1 }))
{
Customer customer = multi.Read<Customer>().Single();
Address address = multi.Read<Address>().Single();
}
con.Close();
}
但这真的有必要吗?有更好的方法吗?我在添加包
MiniProfiler.Providers.SqlServer
后遇到了同样的问题,该包依赖于Dapper.StrongName
包,该包依赖于Dapper
显然,我不想使用已经被引用的MiniProfiler包,因为拥有控制权总是更好的,比如在发布新版本时更新它
解决程序集之间不明确代码的解决方案是为至少一个程序集包指定一个名称,因此,当您希望引用其中一个程序集包时,可以使用别名指定要引用的程序集包
以下是对我有效的解决方案:
为了给程序集Dapper.StrongName
提供一个别名
,我在.csproj
文件中添加了以下内容:
<Target Name="StrongNameAlias" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
<ItemGroup>
<ReferencePath Condition="'%(FileName)' == 'Dapper.StrongName'">
<Aliases>MP_DapperStrongNameAlias</Aliases>
</ReferencePath>
</ItemGroup>
</Target>
因此,现在您可以使用Dapper自由添加代码>,它将不再冲突
这篇文章可能会有所帮助:我不明白编译器的错误。但是我只有一个QueryMultiple
——重载这些参数:IDbConnection cnn,string sql,dynamic param=null,IDbTransaction=null,int?commandTimeout=null,CommandType?commandType=null
@TimSchmelter引用多个DLL时可能会出现问题?dapper不可能用dynamic
指定重载。。至少就我所做的努力而言,我在试图创建这样一个重载时遇到了一个编译错误。解决方案中是否有另一个SqlMapper
-文件?搜索项目范围内的QueryMultiple
。可能是一个旧版本。dapper中肯定没有更多的dynamic
重载:再次设置同一个项目,这次没有DapperExtensions,现在似乎工作正常。
<Target Name="StrongNameAlias" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
<ItemGroup>
<ReferencePath Condition="'%(FileName)' == 'Dapper.StrongName'">
<Aliases>MP_DapperStrongNameAlias</Aliases>
</ReferencePath>
</ItemGroup>
</Target>
using MP_DapperStrongNameAlias::Dapper;