Adding a Dapper Dynamic Output Parameter in VB.NET

.net-4.0 dapper parameters


I'm trying to use Dapper to call a stored procedure that has a couple Output parameters, using VB.NET (and .NET 4.0).

However, it seems I cannot use the DynamicParameters.Add method, because I'm getting the following compiler error:

'Add' is ambiguous because multiple kinds of members with this name exist in class 'Dapper.DynamicParameters'.

...when I try to write the following line:

p.Add("@NewRecordID", DbType:=DbType.Int32, direction:=ParameterDirection.Output)

A quick search tells me this sometimes happens when using a C# library that has multiple methods that differ only in name case (VB.NET being case-insensitive). Searching the Dapper source code for DynamicParameters does show the following two overloads for the Add method, but they both use the same case, and the compiler should be able to discern between the two.

public void Add(string name, object value, DbType? dbType, ParameterDirection? direction, int? size)

public void Add(string name, object value = null, DbType? dbType = null, ParameterDirection? direction = null, int? size = null, byte? precision = null, byte? scale = null)

(I've also tried adding scale:=Nothing to the call to force the second overload, to no avail.)

While I can work around this with the input parameters by passing in an anonymous object to the DynamicParameters constructor, I can't find a way around this when adding the output parameters.

I've checked the project references to ensure there aren't multiple or ambiguous assembly references.

Has anybody encountered this problem before, and found a workaround?

At the moment, the only option I can think of is to re-write the stored procedure call without using Dapper, as implied here.

Popular Answer

From what I can gather, the following are all potential solutions:

  1. Rewrite the Stored Procedure to not use Output parameters. (The option I was able to use in this case.)
  2. Rewrite the code calling the Stored Procedure to use standard ADO.NET.
  3. Rewrite Dapper to use a different overload pattern for DynamicParameters.Add.
  4. Update the project to use .NET 4.5.
  5. Reimplement IDynamicParameter(s) or possibly subclass DynamicParameters

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why