Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to SQL transaction and read ID of inserted record

Can anybody explaoin me how to insert in database with Linq with transaction and read id of that record ?

like image 770
Damir Avatar asked Dec 08 '25 20:12

Damir


1 Answers

If you don't have a DBML setup, then add one to your project.
To add a DBML to your project from Solution Explorer:
Right click on your project -> go to Add new Item -> select LINQ to SQL Classes

To fill the DBML with your tables go to:
View -> Server Explorer
Expand your database and drag your tables onto the dbml viewer

Then if your table is caleld Widget:

DataClasses1DataContext db = new DataClasses1DataContext(myConnection);
db.Widgets.InsertOnSubmit(myWidget);

db.SubmitChanges();

//Here myWidget.Id will be set
like image 166
Brian R. Bondy Avatar answered Dec 10 '25 10:12

Brian R. Bondy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!