Parameterized DAX Queries with ADOMD.NET
connection.Query(query, DynamicParameters) gives a System.NotSupportedException when using DAX with ADOMD.NET. I am on Dapper version 1.50.2. The query when not used with parameters runs fine. The code is:-
IList<dynamic> values = new List<dynamic>() { "true" };
var p = new DynamicParameters();
for (int index = 0; index < values.Count; index++)
p.Add("P" + index, values.ElementAt(index));
dynamic results = connection.Query(query, p);
The query itself:-
"DEFINE VAR __DS0FilterTable1 = FILTER(KEEPFILTERS(VALUES('Calendar'[Last 12 Months Indicator])), ('Calendar'[Last 12 Months Indicator] = @P0)) EVALUATE SUMMARIZECOLUMNS('Calendar'[CalendarMonthNumber], 'Calendar'[Calendar Month], __DS0FilterTable1)";
The exact exception:-
"Message": "An error has occurred.",
"ExceptionMessage": "Specified method is not supported.",
"ExceptionType": "System.NotSupportedException",
"StackTrace": " at Microsoft.AnalysisServices.AdomdClient.AdomdParameter.get_DbType()\r\n at Dapper.DynamicParameters.AddParameters(IDbCommand command, Identity identity)\r\n at Dapper.DynamicParameters.Dapper.SqlMapper.IDynamicParameters.AddParameters(IDbCommand command, Identity identity)\r\n at Dapper.SqlMapper.<>c__DisplayClass151_0.2 paramReader)\r\n at Dapper.SqlMapper.<QueryImpl>d__1251.MoveNext()\r\n at System.Collections.Generic.List1..ctor(IEnumerable1 collection)\r\n at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source)\r\n at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable1 commandTimeout, Nullable1 commandType)\r\n at Dapper.SqlMapper.Query(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable1 commandTimeout, Nullable`1 commandType)\r\n at OEMService.BusinessAccess.Services.DataService.
This would be a bug to file against the provider - accessing .DbType on a parameter is part of the base ADO.NET interface, and it seems they don't support that here. If they fix it on the provider side, Dapper will start working correctly :)