Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework: Database.ExecuteSqlCommand Method

So, I have a basic update statement I am running in my MVC 4 app. I am calling it like so (SQL Server 2008 R2, Entity Framework 5.0):

var requestData = requestInfo.Database.ExecuteSqlCommand("UPDATE TABLE Blah.. ");

The command completes successfully, but sometimes requestData returns 1, sometimes it returns 2. I cannot find any documentation or explanation of what these return values mean. I have looked here:

http://msdn.microsoft.com/en-us/library/gg679456(v=vs.103).aspx

But, it does not give any clear answer.

If someone can toss out a link to something that explains the return values of this command I would be greatly appreciative.

like image 305
Jordan Avatar asked Oct 09 '13 16:10

Jordan


People also ask

What does ExecuteSqlCommand return?

ExecuteSqlCommand. The DbContext exposes a Database property which includes a method called ExecuteSqlCommand . This method returns an integer specifying the number of rows affected by the SQL statement passed to it. Valid operations are INSERT , UPDATE and DELETE .

What is the use of FromSqlRaw method in Entity Framework Core?

Entity Framework Core has a powerful method known as FromSqlRaw() which is used to Execute Raw SQL Queries including Parameterized Queries. This method returns an entity object. The FromSqlRaw() method resides in the Microsoft. EntityFrameworkCore namespace.

Which method is used to retrieve data using SQL query statements in EF?

You can use the LINQ method syntax or query syntax when querying with EDM. Visit LINQ Tutorials to learn LINQ step-by-step. The following sample LINQ-to-Entities query fetches the data from the Student table in the database. As you can see above, we created an instance of the context class SchoolDBEntities .


1 Answers

The command completes successfully, but sometimes requestData returns 1, sometimes it returns 2. I cannot find any documentation or explanation of what these return values mean.

ExecuteSqlCommand will return the number of rows affected by your UPDATE statement.


Testing:

//Update ID 2
using (var context = new Test2Context())
{
    var items = context.MyTestClasses.Where(x => x.Id == 2).Count();
    var rowsAffected = context.Database.ExecuteSqlCommand("UPDATE MyTestClasses SET Name = 'Test2' WHERE Id = 2");
    Debug.WriteLine("--First Test--");
    Debug.WriteLine("items: {0}", items);
    Debug.WriteLine("rowsAffected: {0}", rowsAffected);
}

//Update all
using (var context = new Test2Context())
{
    var items = context.MyTestClasses.Count();
    var rowsAffected = context.Database.ExecuteSqlCommand("UPDATE MyTestClasses SET Name = 'Updated'");
    Debug.WriteLine("--Second Test--");
    Debug.WriteLine("items: {0}", items);
    Debug.WriteLine("rowsAffected: {0}", rowsAffected);
}

Results:

--First Test--
items: 1
rowsAffected: 1
--Second Test--
items: 3
rowsAffected: 3
like image 72
Khan Avatar answered Sep 21 '22 23:09

Khan