简体   繁体   English

实体框架:Database.ExecuteSqlCommand方法

[英]Entity Framework: Database.ExecuteSqlCommand Method

So, I have a basic update statement I am running in my MVC 4 app. 所以,我有一个基本的更新声明,我在我的MVC 4应用程序中运行。 I am calling it like so (SQL Server 2008 R2, Entity Framework 5.0): 我这样称呼它(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. 命令成功完成,但有时requestData返回1,有时返回2.我找不到任何文档或解释这些返回值的含义。 I have looked here: 我看过这里:

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. 如果有人可以链接到解释此命令的返回值的链接,我将非常感激。

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.我找不到任何文档或解释这些返回值的含义。

ExecuteSqlCommand will return the number of rows affected by your UPDATE statement. 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM