views:

59

answers:

3

I am using Entity Framework 3.5, while inserting a record i want to return the unique identifier with entity framework. How to do that?

+3  A: 

After calling savechages the new id will be stored in the entity. so you can retrieve from the entity.

Saravanan I M
+3  A: 

More generally, "To ensure that objects on the client have been updated by data source-side logic, you can call the Refresh method with the StoreWins value after you call SaveChanges.", per MSDN.

http://msdn.microsoft.com/en-us/library/bb738618.aspx

Hightechrider
That's unnecessary, for an ID, anyway. `SaveChanges()` alone is enough.
Craig Stuntz
Hence the phrase "more generally".
Hightechrider
Actually, I have to take back what I wrote. With EF 1 and server-generated GUID IDs, they won't be returned on `SaveChanges` and a `Refresh` *would* be necessary. For `int` or `long` IDs, however, a `SaveChanges` alone is enough.
Craig Stuntz
A: 

One way that I have taken to working is, when sending a record to my DataLayer/Business Logic Layer, is to have the DL/BLL do the update/insert, then retrieve the record back from the DB and return it with a boolean indicating success or failure.

That way, I can immediately refresh the record in the interface with the written one so I can be sure the record actually went in how it should have.

hermiod