11

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

I get the error

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?)'

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?

Dale K
  • 25,246
  • 15
  • 42
  • 71
trenki
  • 7,133
  • 7
  • 49
  • 61
  • 2
    I don't get that compiler error. But i have only one `QueryMultiple`-overload with these parameters: `IDbConnection cnn, string sql, dynamic param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null` – Tim Schmelter Aug 25 '15 at 10:34
  • 1
    @TimSchmelter Looks like it's potentially an issue with referencing multiple DLLs? It shouldn't be possible for dapper to specify an overload with `dynamic` .. at least as far as I've tried, I get a compile error trying to create such an overload. – Rob Aug 25 '15 at 10:36
  • 1
    Maybe. Do you have another `SqlMapper`-file somewhere in the solution? Search for `QueryMultiple` project wide. Maybe an old version. – Tim Schmelter Aug 25 '15 at 10:42
  • 1
    There's definitely no more `dynamic` overload in dapper: https://github.com/StackExchange/dapper-dot-net/commit/b87d958314b1a85f52c7ef37b78d3740154b4d04 – Rob Aug 25 '15 at 10:50
  • 1
    Setup the same project again, this time without DapperExtensions and now it seems to work fine. – trenki Aug 25 '15 at 10:54
  • This conversation here just brought me to my working solution after hours of working to solve the problem, I added a post here, I hope it can be helpful for some. – Mayer Spitz Apr 10 '19 at 20:09
  • odd!! does this work? var multi = con.QueryMultiple(sql, (object)new { id = 1 }) – kenny Apr 10 '19 at 20:20

1 Answers1

8

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.

enter image description here

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.

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:

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>

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;

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

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

Mayer Spitz
  • 2,577
  • 1
  • 20
  • 26