Dapper "Procedure or function sp_XXXX has too many arguments specified."

asp.net-mvc-3 c# dapper micro-orm

Question

I am using Dapper's DynamicParamters object with the template argument to generate arguments with my entities. After I make my call to my stored procedure I am getting the following error "Procedure or function sp_MemberSave has too many arguments specified.". I do have extra properties on some of my entities for business logic, etc. Is there a way to make sure dapper only passes parameters that are actual parameters for the stored procedure? It seems like Dapper would read the stored procedure first and then set the parameters, that way it would only use the ones that are correct. How can I limit the parameters using the template capabilities?

Accepted Answer

Try creating an anonymous type of the appropriate parameters from your object... If your class has A, B, C, and D, and you only need A and B:

DynamicParameters(new { A = entity.A, B = entity.B });

Expert Answer

Can I be very clear on the scenario here? if you just pass the entity (rather than the DynamicParameters) it does do this analysis; i.e. conn.Execute("some sql", someEntity); - it will then only add the members of someEntity that it can see are used in the SQL. There may be some false positives, as it doesn't perform full lexical SQL analysis, so a parameter in comments, i.e.:

-- removed by Fred: where row.Date < @StartDate

will still be included (so in the example above, the member StartDate would be eligible, even though it probably isn't actually needed).

However; DynamicParameters currently trusts the custom implementation. I suppose we could move the parameter analysis check to after this point, but I'd prefer to understand the full scenario first.




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