i\'m using Entity Framework 4.0 and having a silly problem that i can\'t figure out.
I have two tables:
There are two types of associations. Independant association where the related key would only surface as navigation property. Second one is foreign key association where the related key can be changed using foreign key and navigation property. So you can do the following.
//option 1 generic option
var contacttype = new ContactType{Id = 3};
db.ContactTypes.Attach(contacttype);
customer.ContactType = contacttype;
option 2 foreign key option
contact.ContactTypeId = 3;
//generic option works with foreign key and independent association
contact.ContactReference.EntityKey = new EntityKey("container.contactset","contacttypeid",3);
I had this happening when I was editing related objects from two separate contexts at the same time. Example:
DataContext ctxA = new DataContext();
DataContext ctxB = new DataContext();
Author orwell = new Author {Name = "George Orwell" };
ctxA.Add(orwell);
ctxB.Add(new Book {Name = "1984", Author = orwell});
ctxA.SaveChanges();
ctxB.SaveChanges();
My case was a little bit more convoluted (as this is obviously quite stupid) but in essence this was causing the error in my case.
Another strange behavior in my case I have a table without any primary key.EF itself creates composite primary key using all columns i.e.:
<Key>
<PropertyRef Name="ID" />
<PropertyRef Name="No" />
<PropertyRef Name="Code" />
</Key>
And whenever I do any update operation it throws this exception:
The property 'Code' is part of the object's key information and cannot be modified.
Solution: remove table from EF diagram and go to your DB add primary key on table that is creating problem and re-add table to EF diagram it's all now it will have single key i.e.
<Key>
<PropertyRef Name="ID" />
</Key>
Just hoping this helps someone else, after struggling for hours with this because i knew i wasnt updating the PK and i definitely had a PK. I had the PK value databound to a menu bar to show the current PK value and this was causing the issue.
I am using EF 4.0 and WPF and I had similar problem, and .... found the issue that solved it (at least for me) in a very simple way.
Because, like you, I thought it must be simple to update a field in a table (i.e. in your case: Contact) that is referenced to by a foreignkey from another table (i.e. in your case: ContactType).
However, the Error Message: ".... is part of the object's key information and cannot be modified." only appears when you try to update a Primary Key (which wasn't my intention at all).
Had a closer look at the XML code of my EntityModel and found it:
<EntityType Name="Contact">
<Key>
<PropertyRef Name="ID" />
<PropertyRef Name="contactTypeID" /> <!-- This second line caused my problem -->
</Key>
<Property Name="ID" Type="int" Nullable="false" />
...
...
</EntityType>
For some reason (maybe I made some foolish mistake within my database), when Visual Studio autogenerated for me the DataModel from my database, it added in that very table (Contact), where I wanted to update the field (ContactTypeID
) a second PropertyRef
(second line).
I just deleted that second PropertyRef
:
<PropertyRef Name="contactTypeID" />
in both, the store model and the conceptual model and .... issue was solved :-)
Hence, remains like:
<EntityType Name="Contact">
<Key>
<PropertyRef Name="ID" />
</Key>
<Property Name="ID" Type="int" Nullable="false" />
...
...
</EntityType>
Updates and Inserts are now running smoothly like a baby .... :-)
Hence, good idea to check the XML of the datamodel to verify that only your PK is listed as PropertyRef
. Worked for me ... :-)
You should add
db.Entry(contact).State = EntityState.Detached;
After the .SaveChanges();