Page 1 of 1

Auto generated ID when inserting into table using EF

Posted: Tue 09 Nov 2010 09:28
by jlundstocholm
I am using dotConnect 5.7 to connect to our ORACLE database. We have a table with an auto-incremented ID column.

I add new rows to the table using code like

Code: Select all

using (var entities = new  MyEntityModel())
{
    entities.MyMessageTable.AddObject(message);
    insertResult = entities.SaveChanges();
}
I would like to get my hands on the ID that was created in the new row in the table.

How do I do this? There are various posts on this forum asking similar questions, but none that I can find that use the entity model approach described here.

Essentially, I am looking for equivalant tothis post on MSDN entitled

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

Posted: Tue 09 Nov 2010 10:09
by bolek75
We created sequences in combination with triggers in our Oracle database. Before inserting a new record the trigger fetches a autoincrement value from the sequence and sets the value in the autovalue column of the table.

Re: Auto generated ID when inserting into table using EF

Posted: Wed 10 Nov 2010 08:28
by rednose84
jlundstocholm wrote:I am using dotConnect 5.7 to connect to our ORACLE database. We have a table with an auto-incremented ID column.

I add new rows to the table using code like

Code: Select all

using (var entities = new  MyEntityModel())
{
    entities.MyMessageTable.AddObject(message);
    insertResult = entities.SaveChanges();
}
I would like to get my hands on the ID that was created in the new row in the table.

How do I do this? There are various posts on this forum asking similar questions, but none that I can find that use the entity model approach described here.

Essentially, I am looking for equivalant tothis post on MSDN entitled

"How to acquire the ID of last row inserted using Entity Framework"
Hy,

you have to set the StoredGeneratedPattern attribute in the Store part of the model.

After you saveAllChanges() your Model now should be the new one.
kind regards

Posted: Wed 10 Nov 2010 17:47
by Shalex
Please refer to this thread and tell us if this helps.