Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a record with LINQ and C# and return the Primary Key of that record

Tags:

c#

linq

What's the best way to write a LINQ query that inserts a record and then returns the primary key of that newly inserted record using C# ?

like image 951
Ebircsa Avatar asked Nov 06 '08 14:11

Ebircsa


1 Answers

The primary key value will be in that property after the SubmitChanges().

MyTable record = new MyTable();
record.Name = "James Curran";
db.MyTable.InsertOnSubmit(record);
db.SubmitChanges();
Console.WriteLine("record inserted as ID : {0}", record.Id);
like image 94
James Curran Avatar answered Sep 23 '22 10:09

James Curran