ExecuteStoreCommand返回-1,EntityFramework,C#? [英] ExecuteStoreCommand returns -1 , EntityFramework , C#?

查看:324
本文介绍了ExecuteStoreCommand返回-1,EntityFramework,C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得下一个自动递增的主键,这里我理解为使用T-SQL来做到这一点。
所以我写了以下方法:

  public int GetLastNewsID()
{
const string command = @SELECT IDENT_CURRENT('{0}')AS Current_Identity;;
int id = EntityModel.ExecuteStoreCommand(command,News);
return id;
}

但它返回-1,而现在必须是4。 p>

PS:
当我在SQL Management Studio中执行T-SQL以下时,它返回4

 使用T; 
GO
SELECT IDENT_CURRENT('NEWS')AS Current_Identity;
GO


解决方案

您需要使用 ExecuteStoreQuery

  public int GetLastNewsID()
{
const string command = @SELECT IDENT_CURRENT({0})AS Current_Identity;;
var id = EntityModel.ExecuteStoreQuery< decimal>(command,News)。

return Convert.ToInt32(id);
}

SQL查询将返回一个 decimal 所以你需要把它转换成 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屋!

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