DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building a Reusable Framework to Standardize API Ingestion in an On-Prem Lakehouse
  • The Update Problem REST Doesn't Solve
  • Stop Guessing, Start Seeing: A Five -Layer Framework for Monitoring Distributed Systems
  • Integrating AI-Driven Decision-Making in Agile Frameworks: A Deep Dive into Real-World Applications and Challenges

Trending

  • Implementing Observability in Distributed Systems Using OpenTelemetry
  • Stateless JWT Auth Microservice Architecture With Spring Boot 3 and Redis Sentinel
  • Mocking Kafka for Local Spring Development
  • Event-Driven Pipelines With Apache Pulsar and Go
  1. DZone
  2. Coding
  3. Frameworks
  4. How to Return the ID Field After an Insert in Entity Framework?

How to Return the ID Field After an Insert in Entity Framework?

By 
Senthil Kumar user avatar
Senthil Kumar
·
Feb. 22, 13 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
85.6K Views

Join the DZone community and get the full member experience.

Join For Free

There are times when you want to retrieve the ID of the last inserted record when using Entity Framework. For example:


Employee emp = new Employee();

emp.ID = -1;

emp.Name = "Senthil Kumar B";

emp.Expertise = "ASP.NET MVC"

EmployeeContext context = new EmployeeContext();

context.AddObject(emp);

context.SaveChanges();


In the above example , if i need to retrieve the ID of the employee that was inserted , all that i need to do is use the emp.ID property once the data is saved as shown below.


Employee emp = new Employee();

emp.ID = -1;

emp.Name = "Senthil Kumar B";

emp.Expertise = "ASP.NET MVC"

EmployeeContext context = new EmployeeContext();

context.AddObject(emp);

context.SaveChanges();

int empID = emp.ID;


Framework entity

Published at DZone with permission of Senthil Kumar. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building a Reusable Framework to Standardize API Ingestion in an On-Prem Lakehouse
  • The Update Problem REST Doesn't Solve
  • Stop Guessing, Start Seeing: A Five -Layer Framework for Monitoring Distributed Systems
  • Integrating AI-Driven Decision-Making in Agile Frameworks: A Deep Dive into Real-World Applications and Challenges

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook