.NET Zone is brought to you in partnership with:

Senthil Kumar is a Software Engineer who has around 3 years of experience in IT industry. He is currently working as a Software Engineer in Bangalore and works mainly on the Windows or Client Development technologies and has good working experience in C#/.NET, Delphi, Winforms and SQL Server. He is also a Microsoft Technology Certified Professional in ASP.NET. He Blogs at http://www.ginktage.com and http://www.windowsphonerocks.com. He enjoys learning as much as he can about all things related to technologies to get a well-rounded exposure of technologies that surround him. Senthil completed his Master of Computer Applications from Christ College (Autonomous), Bangalore in the year 2009 and is a MCA Rank Holder. He has passion for Microsoft technologies especially Windows Phone development. You can connect with him on Twitter at (http://twitter.com/isenthil) , on Facebook at (http://www.facebook.com/kumarbsenthil) and his blog (www.ginktage.com). Senthil is a DZone MVB and is not an employee of DZone and has posted 132 posts at DZone. You can read more from them at their website. View Full User Profile

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

02.22.2013
| 10171 views |
  • submit to reddit

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;

Published at DZone with permission of Senthil Kumar, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Lukasz Zwierko replied on Fri, 2013/02/22 - 6:31am

@dzone: guys can you please posting such a trivial blog posts? Seriously, the articles level on this zone has seriously decreased over the last 2 years. What next? "hello world" examples? Come on..

Saying that - I have nothing against this concrete post, some people may find it valuable, but DZone should be aming for Eric Lippert kind of bloggers or else just rename it to tutorial.dzone.com or such. 

Robson Castilho replied on Sun, 2013/02/24 - 8:51pm in response to: Lukasz Zwierko

Totally agree.

I didn't believe my eyes when I first read it...

Gonzalo Arrivi replied on Wed, 2013/02/27 - 7:59am

I agree. I clicked this post because I thougth there was something more about this, so you can imagine my surprise.

I think this kind of posts would be really cool in a tutorial section, just as @Lukasz said.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.