Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the next Sequence number in Sql Server Express using Entity Framework?

Now that Sql Server 2012 (including SQL Server Express 2012) has SEQUENCE feature just like Oracle as explained here, here, and here.

I can get the next sequence like so, SELECT NEXT VALUE FOR SeqName

But how do I do that from my code using Entity Framework 5?

like image 940
Rosdi Kasim Avatar asked May 25 '13 19:05

Rosdi Kasim


People also ask

How do I find the next value of a sequence in SQL Server?

SELECT - For each referenced sequence object, a new value is generated once per row in the result of the statement. INSERT ... VALUES - For each referenced sequence object, a new value is generated once for each inserted row in the statement.

How do you find sequential numbers in SQL?

To number rows in a result set, you have to use an SQL window function called ROW_NUMBER() . This function assigns a sequential integer number to each result row.

How do you increment a sequence in SQL?

The value that is used to increment (or decrement if negative) the sequence object's base value for each call to the NEXT VALUE FOR function. If the increment is a negative value the sequence object is descending, otherwise, it is ascending. The increment can not be 0. Specifies the bounds for sequence object.

What are the sequences in EF?

A sequence generates unique, sequential numeric values in the database. Sequences are not associated with a specific table, and multiple tables can be set up to draw values from the same sequence.


1 Answers

I got it working using SqlQuery like so..

int sequence = context.Database.SqlQuery<int>("SELECT NEXT VALUE FOR MySequenceName").FirstOrDefault();
like image 107
Rosdi Kasim Avatar answered Oct 06 '22 20:10

Rosdi Kasim