Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I realize this must be a relatively simple thing to do, but I'm not getting what I'm looking for with Google.

I need to get the record ID of the record I just saved using the Entity Framework. With SQL queries we used "Select @@IDENTITY as 'Identity';"

If anyone can help it would be greatly appreciated.

I'm working with ASP.NET C# 3.5 SP1

Thanks
Posted
Updated 13-Sep-10 22:30pm
v2

"EF automatically populates your business objects with the db-generated values after your call to SaveChanges()."

Example:
MIDL
//create a new Client object
Client c = new Client();
c.Name = "Test";
//save to db
context.AddToClients(c);
context.SaveChanges();

//EF will populate your original object with the generated values
Debug.WriteLine( " Client ID is " + c.ClientID );
 
Share this answer
 
Comments
Vatsal_Desai 26-Mar-11 9:17am    
I am using the same method to get auto generated identity. It's working fine with MS Sql but not working with MySql.

Any Idea???
Actually, EF does populate the just inserted entity with its new ID. I have tested it using the EF provider for SQL Server 2008 and .NET 4.
 
Share this answer
 
this my code where I can save
db.ValueDocument.add(objValueDocument);
db.SaveChanges();
then
immediately i can get the value by using following code
db.ValueDocument.Local[0].ValueDocumentID;
 
Share this answer
 
 
Share this answer
 
v2
There is no such direct method to get the generated Identiy key value after the insert.

Once the Record is saved, u can create another method like SELECT Top 1 from table order by dtimecre desc

I have searched for the same criteria mentioned by u. but dint get any proper solution. so need to follow the traditional method.

hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900