Hello all,
I'm stuck with an optimistic concurrency problem in .NET.
I can edit a record in my test program and save it. That's ok so far. But if I edit the same record again and save it again, I get an optimistic concurrency exception with the following text:
Speicheranweisung für Aktualisieren, Einfügen oder Löschen betraf eine unerwartete Anzahl an Zeilen (0). Möglicherweise wurden einige Entitäten seit dem Laden der Entitäten geändert oder gelöscht. Aktualisieren Sie die ObjectStateManager-Einträge.
Translation:
Store update, insert, or delete statement affected an unexpected number of rows (X). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.
Since I'm the only one working on my local DB, this shouldn't happen.
I'm using
ODAC 11.2 Release 4 (11.2.0.3.0) with Oracle Developer Tools for Visual Studio
in an Entinty Framework 4 project with a
Oracle Database 10g Express Edition Release 10.2.0.1.0
database.
And this is what I'm trying to do:
First I created a table, ...
create table TESTTABLE (
ID RAW(16) not null,
TESTSTRING NVARCHAR2(255),
TESTINT NUMBER(10),
TESTDATE DATE,
TESTTIMESTAMP TIMESTAMP(2),
constraint PK_TESTTABLE primary key (ID)
);
filled it with test data...
insert into TESTTABLE (ID, TESTSTRING, TESTINT, TESTDATE, TESTTIMESTAMP)
values ('01011111111111111111111111111111', 'TestString1b', 11, to_date('15-05-2012 11:22:35', 'dd-mm-yyyy hh24:mi:ss'), '15-MAY-12 11.22.36.05 AM');
insert into TESTTABLE (ID, TESTSTRING, TESTINT, TESTDATE, TESTTIMESTAMP)
values ('02022222222222222222222222222222', 'TestString2b', 22, to_date('15-05-2012 11:22:58', 'dd-mm-yyyy hh24:mi:ss'), '15-MAY-12 11.22.58.77 AM');
and activated optimistic concurrency in my edmx file for all columns of the table. (I've also tested activating optimistic concurrency only for one column.
In that case the exception occures, if I edit that particular column repeatedly.)
Then I loaded the records of the table into a TableGridView control, edited one record, saved the changes, edited the same record again and saved the changes again.
The optimistic concurrency exception occures on the second save.
To save the changes I use the following code...
foreach (ObjectStateEntry ose in _dataContext.ObjectStateManager.GetObjectStateEntries(EntityState.Modified))
{
TESTTABLE tempEntity = (TESTTABLE)ose.Entity;
tempEntity.TESTDATE = DateTime.Now;
tempEntity.TESTTIMESTAMP = DateTime.Now;
}
_dataContext.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
... where _dataContext ist my Entity Framework Context to the data in the database.
I'm trying to automatically set the date/timestamp of the last save operation right before the save operation (only for each modified record).
(of course in a real application I would have only one column for that not two)
If I don't use the foreach loop with the automatic date/timestamp change, it works fine. Is this a bug in ODAC or am I just doing something wrong?
Please help.
Thank you.