DEV Community

Cover image for Get Id of recently added record in Linq.
bikashgosai
bikashgosai

Posted on

3 2

Get Id of recently added record in Linq.

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;
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay