понедельник, 10 января 2011 г.

How to acquire the ID of last row inserted using Entity Framework?


Identity columns get its value after the entity is saved.
Given the following table:
CREATE TABLE T2 ( ID INTEGER PRIMARY KEY AUTOINCREMENT, Value TEXT)
The following code shows you the id for the inserted row:
using (TestEntities entities = new TestEntities())
{
    T2 t2 = new T2();
    t2.Value = "some value";
    entities.AddToT2(t2);
    entities.SaveChanges();

    Console.WriteLine(t2.ID);

}

Комментариев нет: