Changing identity fields

Thread is closed for posting
2 posts, 0 answers
  1. Jurica
    Jurica avatar
    9 posts
    Member since:
    Nov 2013

    Posted 10 May 2014 Link to this post


    I have situation where entity with complex primary key (identities). Unfortunately, one of them is string/nvarchar (it is not in my power to change that). 

    That table is presented on the form for the user so that he can define multiple N:N relationships. Besides identity fields there is one more string/nvarchar field (Description) that is part of the same table.

    When I try to update keys/identity fields I get message: "Unsupported Operation: Change of identity is not supported."

    When I change Description field and try to delete record/object and add the new entity  with same identities in same transaction I get:
    "Instance of 'StanisteSvojtes' with identity '1120049971-A.' already exists in the cache of the object scope."

    i cannot find solution.

    Please advice.

  2. Ralph Waldenmaier
    Ralph Waldenmaier avatar
    202 posts

    Posted 12 May 2014 Link to this post

    Hi Jurica,
    The scenario you are describing is expected. In general, changing the primary key is not supported by design.

    To your second situation. When you are generating a new object with a primary key or loading it from the database, we are storing it in our first level cache of the context. Now when you are deleting the object and call SaveChanges, it will no longer be in the cache of the context and you can create the new one. If you omit the SaveChanges call, then you will receive the mentioned exception. See the following snippet of how it will work.

    var id = Guid.NewGuid();
    var usr = new User(){Id = id};
    var usr2 = new User(){Id = id};

    I hope this information is helpful for you.
    Do come back in case you need further assistance.

    Ralph Waldenmaier
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
Back to Top