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

  • AI Assessments Are Everywhere
  • Logging What AI Agents Do in Salesforce: A Simple One-Object Audit Framework
  • Token Attribution Framework for Agentic AI in CI/CD
  • The Middleware Gap in AI Agent Frameworks

Trending

  • The Trust Problem in Modern SaaS: Why Your Authentication Succeeded, and You Still Got Breached
  • Testing AI-Infused Apps: A Dual-Layer Framework for AI Quality Assurance
  • Reducing RAG Hallucinations With Relationship-Aware Retrieval
  • A Deep Dive into Tracing Agentic Workflows (Part 2)
  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

  • AI Assessments Are Everywhere
  • Logging What AI Agents Do in Salesforce: A Simple One-Object Audit Framework
  • Token Attribution Framework for Agentic AI in CI/CD
  • The Middleware Gap in AI Agent Frameworks

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