Dapper歧义扩展方法 [英] Dapper ambiguous extension methods

查看:105
本文介绍了Dapper歧义扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Dapper作为ORM解决方案进行测试,并且遇到了某些扩展方法(例如 Execute QueryMultiple )的问题:

I am testing Dapper as a ORM solution and ran into a problem with some extension methods like Execute or 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();
}

我收到错误

以下方法或属性之间的调用不明确:'Dapper.SqlMapper.QueryMultiple(System.Data.IDbConnection,字符串,对象,System.Data.IDbTransaction,int?,System.Data.CommandType?)'和' Dapper.SqlMapper.QueryMultiple(System.Data.IDbConnection,字符串,动态,System.Data.IDbTransaction,int?,System.Data.CommandType?)'

The call is ambiguous between the following methods or properties: 'Dapper.SqlMapper.QueryMultiple(System.Data.IDbConnection, string, object, System.Data.IDbTransaction, int?, System.Data.CommandType?)' and 'Dapper.SqlMapper.QueryMultiple(System.Data.IDbConnection, string, dynamic, System.Data.IDbTransaction, int?, System.Data.CommandType?)'

不知道如何正确解决这个问题。 Dapper的示例没有提到这样的问题,只是使用了 QueryMultiple 。我可以使用

and don't know how to properly solve this. The Dapper examples didn't mention such a problem and simply used QueryMultiple. I was able to circumvent the ambiguity using

var multi = con.QueryMultiple(new CommandDefinition(sql, new { id = 1 }))

但这真的有必要吗?有更好的方法吗?

But is that really necessary? Is there a better way?

推荐答案

添加软件包 MiniProfiler之后遇到同样的问题。 Providers.SqlServer ,它取决于 Dapper.StrongName 包,它取决于 Dapper

I bumped into the same problem after I added the package MiniProfiler.Providers.SqlServer, which depends on the Dapper.StrongName package, which depends on Dapper.

显然我不想使用已经引用的MiniProfiler程序包,因为最好总是具有控制权,例如在发布新版本时进行更新。

Obviously I didn't want to use the already referenced package of MiniProfiler, since it is always better to have control, like updating it when a new version is released.

解决程序集之间歧义代码的解决方案是分配一个 extern alias 至少要对其中一个汇编程序包命名,因此当您要引用其中一个汇编程序包时,可以指定您要使用别名引用哪一个。

The solution to resolving ambiguous code between assemblies is to assign an extern alias name/s to at least one of the assembly packages, so when you then want to refer to one of them, you can then specify which one you want to reference by using the alias name.


这里是适用于我的解决方案:

Here is the solution that worked for me:

为装配体赋予别名 code> Dapper.StrongName ,我在 .csproj 文件中添加了以下内容:

To give an alias name to assembly Dapper.StrongName, I added the following to my .csproj file:

<Target Name="StrongNameAlias" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
  <ItemGroup>
    <ReferencePath Condition="'%(FileName)' == 'Dapper.StrongName'">
      <Aliases>MP_DapperStrongNameAlias</Aliases>
    </ReferencePath>
 </ItemGroup>
</Target>

然后,如果要引用它,可以引用程序集命名空间由其在 .cs 文件中新添加的 alies ,其中< a href = https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/operators/namespace-alias-qualifer rel = nofollow noreferrer> ::运算符

And then, if you'd want to refer to that, you can reference the assembly namespace by its newly added alies in .cs file, with the :: operator:

using MP_DapperStrongNameAlias::Dapper;

现在,您可以使用Dapper自由添加

So now you can freely add using Dapper;, and it won't conflict anymore.

此文章可能会有所帮助: C#2.0:使用同一dll的不同版本在一个应用程序中

This Article might be helpful: C# 2.0: Using different versions of the same dll in one application

这篇关于Dapper歧义扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆