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.

tereško
  • 58,060
  • 25
  • 98
  • 150
Jordan
  • 2,992
  • 2
  • 20
  • 29

1 Answers1

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
Khan
  • 17,904
  • 5
  • 47
  • 59
  • 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. – Jordan Oct 09 '13 at 16:15
  • Nothing fancy: UPDATE TABLE SET COLUMN_NAME = '2', OTHER_COLUMN = '3' WHERE ID = '123' (123 would be a valid row ID). – Jordan Oct 09 '13 at 16:40
  • 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. – Khan Oct 09 '13 at 17:07
  • 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? – Jordan Oct 09 '13 at 17:08
  • It should not be possible if the primary key is set to the ID/Constraint is in place. Can you reproduce the case where you're getting a value greater than one? Can you run that same update statement in SSMS and verify it gives you a different Rows Affected count? – Khan Oct 09 '13 at 17:11
  • 8
    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! – Jordan Oct 09 '13 at 17:57
  • Ha! A scenario I wouldn't have thought of. Glad you found it, and happy to help! – Khan Oct 09 '13 at 18:10
  • 1
    @Jordan Thanks for the hint with the trigger ;-) – Obl Tobl Jul 25 '14 at 06:35