You can get this value with a little hack.
Create a function in SQL Server something like this:
CREATE FUNCTION fn_getIdentity(@tbl_name varchar(30)) AS BEGIN IF @tbl_name = 'Employee_tbl' RETURN IDENT_CURRENT('Employee_tbl') ELSE IF @tbl_name = 'Department_tbl' RETURN IDENT_CURRENT('Department_tbl') ELSE RETURN NULL END
Create an entity in your Entity framework to support this feature and use it where you want.
Then use
var nextValue = dbContext.fn_getIdentity("Employee_tbl")
IDENT_CURRENT returns the last incremental value for the identity column. This does not mean MAX + 1, as if your previous transaction generated an identifier value for this column, but was discarded, then you will see the next value that will be generated.
Note that I did not check the syntax correctly, and this syntax is just to present the idea.
However, I would go with the solution provided by Stakx, i.e., SEQUENCE, if you are using SQL Server 2012 or higher, otherwise create a table to implement the SEQUENCE functionality by reserving the identifier when it is constantly created in the table.
source share