1

This is my table:

-- Original table schema
CREATE TABLE [SchoolYear] (
    [Start] datetime NOT NULL,
    [End] datetime NOT NULL,
    [Id] integer PRIMARY KEY ON CONFLICT ABORT AUTOINCREMENT NOT NULL
);

My Entity in the EF designer has StoredGeneratedPattern set to Identity OR Compute and datatype is int64.

Everytime I insert a second SchoolYear object I get this error:

An object with the same key is already in the ObjectStateManager...

Justin
  • 84,773
  • 49
  • 224
  • 367
msfanboy
  • 5,273
  • 13
  • 69
  • 120

1 Answers1

3

Check that the autoincremented property has the StoreGeneratedPattern attribute set to "Identity" in the SSDL part of the model, not in CSDL.
This is a known issue, the conceptual attribute is not taken into account while generating the code.

Devart
  • 119,203
  • 23
  • 166
  • 186
  • its set to Identity in both: SSDL + CSDL so it should work, but it does NOT! – msfanboy Jul 16 '10 at 17:16
  • without recognizing it earlier I have a new exception now: System.InvalidOperationException was unhandled: ...The EntityKey property can only be set when the current value of the property is null... EDIT: Adding a new object to the context works for the first time when I check the table a new row is created, so the autoinc must work. Its something different now! – msfanboy Jul 16 '10 at 17:57
  • What a stupid error that was: (got again a new exception ...) I added always the same object to the context that seemed the problem. Now it works. I could have sworn, that EF v1 allowed me to add as many equal objects I wanted without throwing an exception, EF v1 just avoided adding the dupe object. Seems that changed in EF v4 ? – msfanboy Jul 16 '10 at 18:24
  • Try dotConnect for SQLite (http://devart.com/dotconnect/sqlite). I have made a test and autoincrement column is read successfully into the inserted objects. – Devart Jul 19 '10 at 12:52