How can we get the highest ID number in an empty SQL table ?
e.g:
Assume we have a table with ten rows, but all of rows were deleted , now we wanna get the highest row’s ID !!!
The below code throw an Exception because the table is empty :
var maxID = db.TableNames.Select(q => q.Id).Max();
For SQL Server try the IDENT_CURRENT function.
Note that there can be gaps in identity value sequences because of failed transactions etc, so there is no guarantee that a row was ever committed with this value.