Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the key name of an entity

I understand that they are not supposed to be changed, this is somewhat of a schema migration and only a one-time thing.

I would like to change the key names of entities in my Google App Engine application, effectively deleting and re-crating an entity and updating all references to it.

What is the best way to do this? I'm interesting in hearing anyone's experience with such things.

like image 981
Noah McIlraith Avatar asked Oct 27 '10 10:10

Noah McIlraith


People also ask

How does entity change value of primary key?

You cannot update the primary key through entity framework, since entity framework would not know which database row to update. However, if you really need to do it, you could write a stored procedure that updates the primary key, and then execute the stored procedure from entity framework.

What are entity keys?

An entity key is a property or a set of properties of an entity type that are used to determine identity. The properties that make up an entity key are chosen at design time. The values of entity key properties must uniquely identify an entity type instance within an entity set at run time.

How do you configure a primary key for an entity in Entity Framework fluent API?

Configuring a primary key By convention, a property named Id or <type name>Id will be configured as the primary key of an entity. Owned entity types use different rules to define keys. You can configure a single property to be the primary key of an entity as follows: Data Annotations.


1 Answers

Since changing the key name is functionally identical to creating a new, identical entity with that key, what you want to do is clone the entity with the new key. Here's some code that does just that.

like image 122
Nick Johnson Avatar answered Sep 24 '22 08:09

Nick Johnson