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

  • A Zero-Trust Implementation Framework for Cloud Migrations: Lessons From Enterprise Deployments
  • The Tectonic AI Platform: A Framework for Taming App Sprawl and Data Fragmentation
  • This One Spring Data JPA Pattern Cleaned Up to 3 Years of Repository Debt
  • Designing a Page Object Model + TestNG Hybrid Framework: Patterns That Actually Scale

Trending

  • From Data Movement to Local Intelligence: The Shift from Centralized to Federated AI
  • Microservices Architecture in Production: 7 Engineering Decisions That Determine Success or Failure
  • AI in SRE: A Practical Autonomy Model for Self-Healing Infrastructure
  • A Step-by-Step Guide to Implementing Columnar Tables in SQL Server
  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

  • A Zero-Trust Implementation Framework for Cloud Migrations: Lessons From Enterprise Deployments
  • The Tectonic AI Platform: A Framework for Taming App Sprawl and Data Fragmentation
  • This One Spring Data JPA Pattern Cleaned Up to 3 Years of Repository Debt
  • Designing a Page Object Model + TestNG Hybrid Framework: Patterns That Actually Scale

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