Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The property 'Id' is part of the object's key information and cannot be modified

i'm using Entity Framework 4.0 and having a silly problem that i can't figure out.

I have two tables:

  1. Contact: Id (primary key), Value, ContactTypeId (foreign key to ContactType)
  2. ContactType: Id (Primary key), Type (Home, Cell, Work, etc.)

Entity Framework created the following two entities:

  1. Contact: Id, Value, ContactType (Navigation Property)
  2. ContactType: Id, Type, Contact (Navigation Property)

I'm using the following code to get the contact and update the contact type for that particular contact:

Contact contact = dbContext.Contacts.Single(c => c.Id == 12345); contact.ContactType.Id = 3; 

Throws the following exception:

The property 'Id' is part of the object's key information and cannot be modified. 

It looks so simple! I don't get it!

like image 245
Amito Avatar asked Jul 06 '10 15:07

Amito


People also ask

What is part of an object's key information and Cannot be modified?

"The property 'Id' is part of the object's key information and cannot be modified."

Is part of a key and so Cannot be modified or marked as modified to change the principal?

{property}' is part of a key and so cannot be modified or marked as modified. To change the principal of an existing entity with an identifying foreign key, first delete the dependent and invoke 'SaveChanges', and then associate the dependent with the new principal.

How do I change my primary key in Entity Framework?

You cannot update a primary key, but that is not a limitation of entity framework, but a very fundamental rule of database development. A primary key is assigned once to a row in a table and makes this row unique. Maybe you can update the key in some ways, but this violates definitely the definition of a primary key.


2 Answers

This problem happens because you are referencing the same object more than once. This is not a limitation of EF, but rather a safety feature to ensure you are not inserting the same object with two different IDs. So to achieve what you are trying to do, is simply create a new object and add the newly created object to the database.

** This issue often happens inside loops. If you are using a while or foreach loop, make sure to have the New Created Object INSIDE the loop body.

try this:

Contact contact = dbContext.Contacts.Single(c => c.contactTypeId == 1234); contact.contactTypeId = 4; dbContext.AddObject(contact); dbContext.SaveChanges(); 
like image 155
AL-Tamimi Avatar answered Sep 21 '22 00:09

AL-Tamimi


The entity that was created by the framework doesn't have a contact.ContactTypeId property. It automatically removed it and created the ContactType association inside the Contact entity.

The way to get it to work, as you suggested, is to create a ContactType object by querying the database and assigning it to contact.ContactType. For example:

Contact contact = dbContext.Contacts.Single(c => c.Id == 12345); ContactType contactType = dbContext.ContactType.Single(c => c.Id == 3); contact.ContactType = contactType; 
like image 25
Amito Avatar answered Sep 20 '22 00:09

Amito