Lately I've been working on stored procedure and encountered 1 strange problem.
First, I was able to successfully call a stored procedure from the database via:
IList<XXXViewModel> XXXList = _context.Database.SqlQuery("spXXX").ToList();
But when I needed to pass parameters it failed:
var parameters = new List<SqlParameter>(); parameters.Add(new SqlParameter("param1", param1Value)); parameters.Add(new SqlParameter("param2", param2Value)); IList<XXXViewModel> XXXList = _context.Database.SqlQuery<XXXViewModel>("spXXX @param1, @param2", parameters).ToList();
And I got the ff, error:
No mapping exists from object type System.Collections.Generic.List`1[[System.Data.SqlClient.SqlParameter, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] to a known managed provider native type.
Note that I've also tried:
_context.Database.ExecuteSqlCommand<EXEC XXXViewModel>("spXXX @param1, @param2", parameters).ToList();
But got the same result :-(.
Also I've tried calling, by specifying each of the parameters:
IList<XXXResult> query = Context.Database.SqlQuery<XXXResult>("SP @paramA, @paramB, @paramC", new SqlParameter("paramA", "A"), new SqlParameter("paramB", "B"), new SqlParameter("paramC", "C")).ToList();
Anyone has any idea?
In case someone else comes across this...
I created the parameters as a List and then in the SqlQuery call I passed it with a .ToArray(). Worked for me. Here's the modified code below...
var parameters = new List<object>(); parameters.Add(new SqlParameter("param1", param1Value)); parameters.Add(new SqlParameter("param2", param2Value)); IList<XXXViewModel> XXXList = _context.Database.SqlQuery<XXXViewModel>("spXXX @param1, @param2", parameters.ToArray()).ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With