10

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.

1 Answer 1

20

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
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the response. But I'm not sure if that is correct. In my statement I am specifying a row id. If SQL allows itself to update more than one row after explicitly telling to only update 1 that would be a huge problem.
Nothing fancy: UPDATE TABLE SET COLUMN_NAME = '2', OTHER_COLUMN = '3' WHERE ID = '123' (123 would be a valid row ID).
I ran a test to verify that it is Rows Affected. Can you reproduce your issue of it not correlating? Please see my edited answer.
Thanks for your testing. But, if I am specifying a row ID, how could it be possible that it is updating more than 1 row?
I went back and found someone had a trigger setup on that table. It was inserting a record in another table therefore, returning an affected row count of 2. Thanks for your help!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.