您可以創建在SQL Server中選擇這樣的一個序列值一個簡單的存儲過程:
CREATE PROCEDURE dbo.GetNextSequenceValue
AS
BEGIN
SELECT NEXT VALUE FOR dbo.TestSequence;
END
,然後可以導入存儲過程到實體框架的EDMX模型,並調用存儲程序和獲取這樣的序列:
// 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查詢:
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;
}
}
的可能的複製[如何獲得使用實體框架在SQL Server Express的下一個序列號?](http://stackoverflow.com/questions/16753149/how-to-get-the-next-sequence -number-in-sql-server-express-using-entity-framework) – 2016-01-23 19:41:28