Page 1 of 1

EF 4 and EntityObject's ID not updating after insert

Posted: Wed 05 May 2010 14:11
by andrzej.stoparczyk
Hello all,

I have a problem with forcing Entity Framework 4 to update sequenced ID in EntityObject after inserting.

I'm using following components:
- dotConnect for Oracle 5.60 Professional Trial,
- Visual Studio 2010 with Entity Framework 4,
- Oracle 11gR1 database.

Tables in database have their corresponding sequences and BeforeInsert triggers defined.
ID columns in EF model all have attribute StoreGeneratedPattern set to Identity.

I'm doing the following:
1. create specific EntityObject using CreateObject method of ObjectContext,
2. I'm leaving ID field unset (it's value is 0 by default),
3. I'm adding EntityObject to EntitySet,
4. and call SaveChanges.

Record is created in database with valid sequenced ID value (for example: 12).
[b]EntityObject's ID is still set to 0.[/b]

Question:
What can be done to force EF to refresh ID value after calling SaveChanges?

Thank you in advance.

PS. I went through the forum and found that I'm doing it right but it seems to be not working for me. Maybe I'm missing something?
PS.2. I have investigated SQL statement logged by dbMonitor and there were only insert statements. I would guess there should be some select added to insert statement for retreiving new ID.

Posted: Wed 05 May 2010 16:53
by AndreyR
Just a guess: maybe you have performed the model updates after setting the AutoGeneratedPattern in SSDL?
All manual changes made to SSDL are discarded by the Update From Database wizard.

Posted: Thu 06 May 2010 09:06
by andrzej.stoparczyk
Thanks for your answer.
That was not the case though.

Fortunately I managed to resolve this issue and it works fine now.

Solution:

I have been using VS 2010 EF model editor to manage my database model. I've used properties window to set ID's StoreGeneratedPattern attribute to Identity.

After looking closer into EDMX file in XML editor it came out that VS has set SGP attribute in edmx:ConceptualModels this way:

I found that in edmx:StorageModels ID property tag was left this way:

So, I added StoreGeneratedPattern="Identity" to this tag and everything magically started to work.

I don't know why VS set this property like above. Looks like manual tweaks in XML are still needed.

Posted: Thu 06 May 2010 09:35
by AndreyR
Yes, it is a known limitation of VS 2010.