该命令成功完成,但有时 requestData 返回 1,有时返回 2。我找不到任何文档或解释这些返回值的含义。
ExecuteSqlCommand
将返回受您的UPDATE
语句影响的行数。
测试:
//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);
}
结果:
--First Test--
items: 1
rowsAffected: 1
--Second Test--
items: 3
rowsAffected: 3