This is a migrated thread and some comments may be shown as answers.

Changing identity fields

1 Answer 65 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jurica
Top achievements
Rank 1
Jurica asked on 10 May 2014, 10:38 PM
Hi,

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.1.1.1.1.-696' already exists in the cache of the object scope."

i cannot find solution.

Please advice.





1 Answer, 1 is accepted

Sort by
0
Ralph Waldenmaier
Telerik team
answered on 12 May 2014, 07:44 AM
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};
ctx.Add(usr);
ctx.SaveChanges();
 
ctx.Delete(usr);
ctx.SaveChanges();
 
var usr2 = new User(){Id = id};
ctx.Add(usr2);
ctx.SaveChanges();

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


Regards,
Ralph Waldenmaier
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
Tags
Development (API, general questions)
Asked by
Jurica
Top achievements
Rank 1
Answers by
Ralph Waldenmaier
Telerik team
Share this question
or