C# 实体框架:Database.ExecuteSqlCommand 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19276996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:34:32  来源:igfitidea点击:

Entity Framework: Database.ExecuteSqlCommand Method

c#asp.net-mvcsql-server-2008entity-framework

提问by Jordan

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):

所以,我有一个基本的更新语句,我在我的 MVC 4 应用程序中运行。我这样称呼它(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:

命令成功完成,但有时 requestData 返回 1,有时返回 2。我找不到任何文档或解释这些返回值的含义。我看过这里:

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

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.

如果有人可以抛出一个链接来解释这个命令的返回值,我将不胜感激。

采纳答案by Khan

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.

命令成功完成,但有时 requestData 返回 1,有时返回 2。我找不到任何文档或解释这些返回值的含义。

ExecuteSqlCommandwill return the number of rows affected by your UPDATEstatement.

ExecuteSqlCommand将返回受您的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