Get Id of recently added record in Linq.
bikashgosai
Posted on January 13, 2022
After you commit your object into the db the object receives a value in its ID field.
So:
myObject.Field1 = "value";
// Db is the datacontext
db.MyObjects.InsertOnSubmit(myObject);
db.SubmitChanges();
//in case of Repository pattern
db.InsertorUpdate(myObject);
db.commit();
// You can retrieve the id from the object
int id = myObject.ID;
💖 💪 🙅 🚩
bikashgosai
Posted on January 13, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.