none
Property with StoreGeneratedPattern set to Identity is not updted after SaveChanges() RRS feed

  • Question

  • I have a method which writes log information in Db. This method is called in a loop. On the second insert I get the InvalidOperationException:

    The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges.

    I checked both SSDL and CSDL: they are Ok. StoreGeneratedPattern is set to Identity. DB is also OK - corresponding id property is identity.

    What I found is that after

    contex.LogHystory.AddObject(logEntry);
    context
    .SaveChanges();

    logEntry.Id still equals 0. So after next call we got two entries with the same Id. Why isn't Identity property updated after SaveChanges ?

    Thursday, November 17, 2011 6:57 AM

Answers

All replies

  • Hi,

    Are you sure the Primary Key column in the database has an IDENTITY specification? I can't really see any reason for getting this message if the PK column has IDENTITY and the SSDL/CSDL has StoreGeneratedPattern set to identity.

    Try profiling your database and run the code and see what the SQL passed to the database looks like.

    Also, can you post the definition of the create table definition in the database and the xml section of your entity from both CSDL and SSDL?

     


    --Rune

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, November 17, 2011 7:31 AM
  • 200% Checked several times.

    By the way, I use mapping to stored procedure and I found this question: http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/ef49bae2-0efd-48a8-925d-40609ac150a0/

    But my proc does not contain parameter for identity property so it's ok too.

    Thursday, November 17, 2011 7:36 AM
    • Marked as answer by Pavel Voronin Thursday, November 17, 2011 7:49 AM
    Thursday, November 17, 2011 7:49 AM
  • Aha.. Important information that you use stored procedures :)

    The problem you experience may be that the @@IDENTITY isn't conaining the last insert identity you did in your code and this causes your problems.

    There are two things I would recommend to you to try:
    1. Call the INSERT INTO statement in your procedure right before your procedure ends
    2. If the first isn't possible, store the @@IDENTITY value right to a variable in your procedure right after your INSERT INTO statement and then return it from your procedure by calling SELECT @MyNewId as NewId and in your procedure mapping on the entity, map the NewId result to the Id column.

     


    --Rune

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, November 17, 2011 8:03 AM
  • Is not ScopeIdentity preferrable over @@Identity ?

    @@Identity can be affected by triggers, ScopeIdentity should be safer, imho.

    Thursday, November 17, 2011 8:27 AM
  • Well, you are absolutly right :)

    I currently work with sybase and it only has @@IDENTITY so i forgot about scope_identity..


    --Rune

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, November 17, 2011 8:41 AM