ExecuteStoreCommand返回

编程入门 行业动态 更新时间:2024-10-27 07:19:38
ExecuteStoreCommand返回-1,EntityFramework,C#?(ExecuteStoreCommand returns -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。

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

USE T; GO SELECT IDENT_CURRENT ('NEWS') AS Current_Identity; GO

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

最满意答案

您需要使用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); }

SQL查询将返回一个decimal因此您需要将其转换为int 。

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.

更多推荐

T-SQL,int,id,返回,电脑培训,计算机培训,IT培训"/> <meta name="description&q

本文发布于:2023-08-03 18:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1395684.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ExecuteStoreCommand

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!