Entity Framework: Database.ExecuteSqlCommand Method

So, I have a basic update statement that I run in my MVC 4 application. I call it that (SQL Server 2008 R2, Entity Framework 5.0):

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

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

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

But this does not give a clear answer.

If someone could throw a link to something that explains the return values ​​of this command, I would really appreciate it.

+7
c # asp.net-mvc sql-server-2008 entity-framework
source share
1 answer

The command succeeds, 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 .


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 
+15
source share

All Articles