Jeremy
Jeremy

Reputation: 46410

Linq to Sql: Can I return the Identity_Scope after an insert?

After I do an insert using linq to sql, can I get the Identity_scope value back if my table has an identity column?

Upvotes: 4

Views: 1044

Answers (1)

Christian C. Salvadó
Christian C. Salvadó

Reputation: 827704

Linq to SQL does the job for you, the identity value it's available just after the SubmitChanges method is called.

var entity = new Entity();
// ...
ctx.Entities.InsertOnSubmit(entity);
ctx.SubmitChanges();
// Here you can use the generated value of yout identity column

entity.Id;

Upvotes: 6

Related Questions