Obtain the Identity of a New Row Using LINQ

It's actually pretty easy and user friendly to obtain the value of the identity column using LINQ. After you create your object and insert it on submit, you can call the identity column's property on your object. For instance:

Dim db As New BlogDataContext()
db.BlogPosts.InsertOnSubmit(MyPost)
db.SubmitChanges()
Dim IdentityValue As Integer = MyPost.PostID

In this example, the IdentityValue variable would be assigned to the PostID of the post that was just submitted.

Published Tuesday, May 20, 2008 2:05 PM by Jason N. Gaylord

Comments

# re: Obtain the Identity of a New Row Using LINQ

Tuesday, June 17, 2008 5:41 PM by tom

This works great however in a multiuser environment, the identity values SOMETIMES gets returned from a different record entered by another user, who was also entering data at the same time...any ideas how to make this work in multiuser environment?

Thanks!

Leave a Comment

(required) 
(required) 
(optional)
(required)