Hello, I'm wondering if there is a way to retrieve the identity value from an identity column before saving a new register.

When I run this query: "

SELECT IDENT_CURRENT ('alpa_Animais') AS AnimalID

" directly using the SQL Server Management Studio I get this value.

Does anyone know if there is a way to get this value from a method inside a web app using Linq?

Thank you

Josi

You can't get the value of an identity column before an insert reliably... so this sounds like a design issue. It is possible that someone can insert a record before between you getting the ident current and your insert thus it would be wrong.

What are you really trying to do here? Maybe there is another way to go about things.

sknake you didn't read OP's question. He/She has no problem to fetch value of identity column before insert a row into a table.

SELECT IDENT_CURRENT ('tablename')

will retrieve latest value of an identity column.

@josi.martarelli>> Does anyone know if there is a way to get this value from a method inside a web app using Linq?
-- answer is Yes.

sknake you didn't read OP's question. He/She has no problem to fetch value of identity column before insert a row into a table.

SELECT IDENT_CURRENT ('tablename')

will retrieve latest value of an identity column.

@josi.martarelli>> Does anyone know if there is a way to get this value from a method inside a web app using Linq?
-- answer is Yes.

No I read the OP's requirement and saw they were setting the stage for race conditions to occur in the database (which is a bad idea) so I asked for clarification....

This article has been dead for over six months. Start a new discussion instead.