ExecuteStoreCommand returns -1 , EntityFramework , C#?

◇◆丶佛笑我妖孽 提交于 2019-12-08 03:17:00

问题


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

回答1:


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.




回答2:


Read part about return value:

http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.executestorecommand.aspx



来源:https://stackoverflow.com/questions/7750587/executestorecommand-returns-1-entityframework-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!