Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 316
czetsuya Avatar asked Feb 05 '12 14:02

czetsuya


1 Answers

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();  
like image 107
Edyn Avatar answered Sep 27 '22 18:09

Edyn