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

  • 12 Factor Framework for Building Secure and Compliant Cloud Applications
  • The Inter-Agent Protocol Problem
  • How Agent Frameworks Solve Human-in-the-Loop
  • AI Assessments Are Everywhere

Trending

  • From Polling to PubSub: Building an Asynchronous OPC UA Stack in Python
  • Building Your API Gateway From OpenAPI Specs: A Spec-Driven Approach
  • Conversational Risk Accumulation: Stateful Guardrails Beyond Single-Turn LLM Checks
  • What Cloud Engineers Actually Need to Know About AI Infrastructure
  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

  • 12 Factor Framework for Building Secure and Compliant Cloud Applications
  • The Inter-Agent Protocol Problem
  • How Agent Frameworks Solve Human-in-the-Loop
  • AI Assessments Are Everywhere

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