Read Inserted ID in LINQ- Similar to Scope_Identity

Inorder to read the primary key that has been inserted in to the database using linq simply read the property from the LINQ entity directly after SubmitChanges().

Here is an Example-

 

DBDataContext context=new DBDataContext();

User newUser=new User();

newUser.UserName=”someuser”;

newUser.Password=”somepwd”;

newUser.FirstName=”Some”;

newUser.LastName=”User”;

context.Users.InsertOnSubmit(newUser);

context.SubmitChanges();

return newUser.UserId // which will be set





 



Cheers

0 Responses to "Read Inserted ID in LINQ- Similar to Scope_Identity"

Post a Comment