6

I am trying to delete multiple rows from the table using linq's ExecuteStoreQuery method like this

 string query = "delete from IMPORTStatistics where districtid='" + districtId + "'";
 db.ExecuteStoreQuery<int>(query);

but it is throwing this exception

"The data reader has more than one field. Multiple fields are not valid for EDM primitive types."

What am I doing wrong?

Just for the information, I am using MySql.

Pawan Nogariya
  • 8,330
  • 12
  • 52
  • 105

2 Answers2

10

Given that you're executing a delete command (not a query), I think you should be using ExecuteStoreCommand instead of ExecuteStoreQuery.

Additionally, you should definitely be using parameters instead of putting the ID directly into the command.

string command = "delete from IMPORTStatistics where districtid={0}";
int rowsDeleted = db.ExecuteStoreCommand(command, districtId);
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • So the parameter would be in paranthesis {0} and not in general syntax of database variables like @DistrictId? – Pawan Nogariya Jan 25 '13 at 11:28
  • @PawanNogariya: That's a wider discussion - if you look into Entity Framework in more detail, I believe both are valid, but possibly in different contexts. (e.g. depending on whether you provide `SqlParameter`s or just the values.) – Jon Skeet Jan 25 '13 at 11:30
  • hmm.. ok. Then I should go in entity framework detail a little :) Thanks! – Pawan Nogariya Jan 25 '13 at 11:32
  • Then, how can we catch the result of the query? I run a stored proc through ExecuteStoreCommand but the result is -1 regardless of what the stored proc returns. And if I try using ExecuteStoreQuery, I get this same error. – Spiky Dec 13 '13 at 20:41
  • @MathieuLeblanc: Well what is your stored proc doing? It sounds like it may be worth asking a new question. – Jon Skeet Dec 13 '13 at 22:18
  • @JonSkeet you're right. In the end, I solved it with a hack : I modified the SP to not only return an integer, but also Select it, so I can grab the result with an ExecuteStoreQuery. Thanks for your reply :-) – Spiky Dec 14 '13 at 17:03
0

This is really helpful link after goggling I found this

http://welcometoaspdotnet.blogspot.com/2012/08/execute-stored-procedure-with-entity.html

thx

Usman Younas
  • 1,323
  • 15
  • 21