'No mapping exists from object type System.Collections.Generic.List when executing stored proc with parameters in EF 4.3
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?
Solution 1:[1]
You need to pass each parameter to the method (ie You can't pass a list)
IList<XXXViewModel> XXXList =
_context.Database.SqlQuery<XXXViewModel>("spXXX @param1, @param2",
new SqlParameter("param1", param1Value),
new SqlParameter("param2", param2Value)).ToList();
Solution 2:[2]
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();
Solution 3:[3]
The solution for this problem (in my case was)
var stuff = db.Database.SqlQuery<SomeEntityType>(query, parms);
Where query was a string that had parameters inserted such as @Name etc. The parms variable was a List of SQLParameters. SQL doesn't like generic lists....
SQL must have an array of SQLParameters sent as and object[] and not a list of generic type.
var stuff = db.Database.SqlQuery<SomeEntityType>(query, parms.ToArray());
Solution 4:[4]
In my case parameter's SQL type and handling null values solved this problem. It was throwing same exception No mapping exists from object type System.RuntimeType to a known managed provider native type. for this also
var parameter1 = new SqlParameter("parameter1", typeof(string));
var parameter2 = new SqlParameter("parameter2", typeof(string));
var parameter3 = new SqlParameter("parameter3", typeof(string));
parameter1.Value = string.IsNullOrEmpty(parameter1Value) ? (object)DBNull.Value : parameter1Value;
parameter2.Value = string.IsNullOrEmpty(parameter2Value) ? (object)DBNull.Value : parameter2Value;
parameter3.Value = string.IsNullOrEmpty(parameter3Value) ? (object)DBNull.Value : parameter3Value;
http://karim-medany.blogspot.ae/2014/02/no-mapping-exists-from-object-type.html
Solution 5:[5]
didn't state SQL Server version, but Erland Sommarskog has an article on how to use Table-Valued Parameters in SQL Server and .NET.
http://www.sommarskog.se/arrays-in-sql-2008.html
Able to pass a variable amount of arguments from client using a single parameter.
Solution 6:[6]
If the stored procedure is only for update/insert, the following can be used as well.
string cmd = Constants.StoredProcs.usp_AddRoles.ToString() + " @userId, @roleIdList";
int result = db.Database
.ExecuteSqlCommand
(
cmd,
new SqlParameter("@userId", userId),
new SqlParameter("@roleIdList", roleId)
);
Solution 7:[7]
If you're initializing the List at the same place you can use Array instead of List
var parameters = new 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();
Solution 8:[8]
If you are using dapper please check the Json serialize is properly added JsonConvert.SerializeObject(**ClassName**)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Eranga |
| Solution 2 | Edyn |
| Solution 3 | JWP |
| Solution 4 | Ali Umair |
| Solution 5 | darkstar |
| Solution 6 | Armali |
| Solution 7 | Mihir Dave |
| Solution 8 | Chamin Thilakarathne |
