实体框架:Database.ExecuteSqlCommand方法 [英] Entity Framework: Database.ExecuteSqlCommand Method

查看:1821
本文介绍了实体框架:Database.ExecuteSqlCommand方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有我运行一个基本的更新语句在我的MVC应用程序4。我喜欢叫它这样(SQL Server 2008 R2中,实体框架5.0):

  VAR =的RequestData requestInfo.Database.ExecuteSqlCommand(UPDATE TABLE布拉赫..);

该命令成功完成,但有时返回的RequestData 1,有时它返回2.我不能找到什么这些返回值意味着任何文件或解释。我在这里看:

<一个href=\"http://msdn.microsoft.com/en-us/library/gg679456(v=vs.103).aspx\">http://msdn.microsoft.com/en-us/library/gg679456(v=vs.103).aspx

但是,它并没有给出任何明确的答案。

如果有人能折腾出一个链接的东西,说明这个命令的返回值,我将大大AP preciative。


解决方案

  

该命令成功完成,但有时回报的RequestData
  1,有时它会返回2.我无法找到任何文件或
  什么这些返回值的意思解释。


ExecuteSqlCommand 将返回受你的更新语句。

的行数

测试

  //更新ID 2
使用(VAR上下文=新Test2Context())
{
    VAR项目= context.MyTestClasses.Where(X =&GT; x.Id == 2).Count之间的();
    VAR的RowsAffected = context.Database.ExecuteSqlCommand(集名='Test2的UPDATE MyTestClasses WHERE ID = 2);
    的Debug.WriteLine( - 第一Test--);
    的Debug.WriteLine(项目:{0},项目);
    的Debug.WriteLine(的RowsAffected:{0}的RowsAffected);
}//全部更新
使用(VAR上下文=新Test2Context())
{
    变种项= context.MyTestClasses.Count();
    VAR的RowsAffected = context.Database.ExecuteSqlCommand(UPDATE MyTestClasses集名称='更新');
    的Debug.WriteLine( - 二Test--);
    的Debug.WriteLine(项目:{0},项目);
    的Debug.WriteLine(的RowsAffected:{0}的RowsAffected);
}

结果

   - 第一Test--
项目:1
的RowsAffected:1
--Second Test--
项目:3
的RowsAffected:3

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.

解决方案

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

这篇关于实体框架:Database.ExecuteSqlCommand方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆