ExecuteStoreCommand返回-1的EntityFramework,C#? [英] ExecuteStoreCommand returns -1 , EntityFramework , C#?
本文介绍了ExecuteStoreCommand返回-1的EntityFramework,C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获得下一个自动递增的主键,的在这里,我理解为使用T-SQL做这件事。
所以我写了下面的方法:
公众诠释GetLastNewsID()
{
常量字符串命令= @SELECT IDENT_CURRENT('{0}')AS Current_Identity;;
INT ID = EntityModel.ExecuteStoreCommand(指挥,新闻报);
返回ID;
}
但它返回-1,而它必须是4现在。
PS:
当我在SQL Management Studio中执行以下T-SQL,它会返回4
使用笔;
GO
选择IDENT_CURRENT(新闻)AS Current_Identity;
GO
解决方案
您需要使用<一个HREF =http://msdn.microsoft.com/en-us/library/dd487208.aspx相对=nofollow> ExecuteStoreQuery
公众诠释GetLastNewsID()
{
常量字符串命令= @SELECT IDENT_CURRENT AS({0})Current_Identity;;
VAR ID = EntityModel.ExecuteStoreQuery<&小数GT;(指挥,新闻报)第一()。
返回Convert.ToInt32(ID);
}
SQL查询将返回小数
,所以你需要将其转换为 INT
。
I wanna get the next automatically incrementing primary key, Here I understood to use T-SQL for doing that. So I wrote the following method :
public int GetLastNewsID()
{
const string command = @"SELECT IDENT_CURRENT ('{0}') AS Current_Identity;";
int id = EntityModel.ExecuteStoreCommand(command, "News");
return id;
}
but it returns -1, whereas it must be 4 right now.
P.S: When I execute below T-SQL in SQL Management Studio , it returns 4
USE T;
GO
SELECT IDENT_CURRENT ('NEWS') AS Current_Identity;
GO
解决方案
You need to use ExecuteStoreQuery
public int GetLastNewsID()
{
const string command = @"SELECT IDENT_CURRENT ({0}) AS Current_Identity;";
var id = EntityModel.ExecuteStoreQuery<decimal>(command, "News").First();
return Convert.ToInt32(id);
}
The SQL query will return a decimal
so you need to convert it to int
.
这篇关于ExecuteStoreCommand返回-1的EntityFramework,C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文