更新时间:2022-12-09 11:50:00
您可以创建一个简单的存储程序中的SQL Server,它选择这样的一个序列值:
You can create a simple stored procedure in SQL Server that selects the next sequence value like this:
CREATE PROCEDURE dbo.GetNextSequenceValue
AS
BEGIN
SELECT NEXT VALUE FOR dbo.TestSequence;
END
然后可以导入存储过程到实体框架的EDMX模型,并调用存储过程并获取这样的序列:
and then you can import that stored procedure into your EDMX model in Entity Framework, and call that stored procedure and fetch the sequence value like this:
// get your EF context
using (YourEfContext ctx = new YourEfContext())
{
// call the stored procedure function import
var results = ctx.GetNextSequenceValue();
// from the results, get the first/single value
int? nextSequenceValue = results.Single();
// display the value, or use it whichever way you need it
Console.WriteLine("Next sequence value is: {0}", nextSequenceValue.Value);
}
更新:其实,你可以跳过存储程序和公正运行的EF背景下,这个原始的SQL查询:
Update: actually, you can skip the stored procedure and just run this raw SQL query from your EF context:
public partial class YourEfContext : DbContext
{
.... (other EF stuff) ......
// get your EF context
public int GetNextSequenceValue()
{
var rawQuery = Database.SqlQuery<int>("SELECT NEXT VALUE FOR dbo.TestSequence;");
var task = rawQuery.SingleAsync();
int nextVal = task.Result;
return nextVal;
}
}