i'm using Entity Framework 4.0 and having a silly problem that i can't figure out.
I have two tables:
Entity Framework created the following two entities:
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!
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;