Based on MSDN :
For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists in an inserted or updated table, the return value includes the number of rows affected by both the insert and update operations, as well as the number of rows affected by the trigger or triggers. For all other types of operators, the return value is -1. If a rollback occurs, the return value is -1.
So ExecuteSqlCommand returns int for queries like Insert , Update , Delete , which means the number of rows affected by one query. Therefore, ExecuteSqlCommand not suitable for queries.
This is also a common problem because the Entity Framework cannot support stored return values from the box, and this is because EF is an ORM and not a replacement for SQL. Check the following link for a similar problem in the first model :
Retrieving data from a stored procedure using the Entity Framework
And this is with ExecuteNonQuery :
ExecuteNonQuery returns -1 when using sql COUNT, despite the query string
Solution : for queries you need to use the Database.SqlQuery<TElement> method:
var ProductList = db.Database.SqlQuery<string>("exec xml_test").ToList();
S. Akbari
source share