The Property 'Id' Is Part of the Object's Key Information and Cannot Be Modified

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

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;

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

As explained in the answer by 'Tone' this error occurs because EF is trying to update all known entities that have changed, even if you do not explicitly call:

customerDataContext.Entry(newCustomerLookup).State = EntityState.Added;
customerDataContext.SaveChanges();

In my case the data that is stored in the DB is encrypted. In my business logic I need to un-encrypt a customer, perform some business logic then update a customerLookUp record. By un-encrypting the customer record I have effectively changed it, which is what causes the error when I try and save the customerLookup record.

Solution:

There are a few different ways to solve this problem. One way is to create a second data-context object. You would use the first one to get the record and un-encrypt it, and then the second one will be used to save the entity to the db. Though this works, I find it to be a bit of a messy solution.

Instead of un-encrypting the record that comes from the dbcontext, I copied the data as it is in to a new object. Then I un-encrypt that new object, this way I never change the record, so when I call saveChanges() the only changes EF will make are to the record I explicitly changed.

This concept is known as the "Data Transfer Object Pattern" you can read more about it here:

https://learn.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-5

https://www.exceptionnotfound.net/entity-framework-and-wcf-mapping-entities-to-dtos-with-automapper/

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

See the answer from yildizm85 to this question: entity framework not working on table without identity column

"Entity Framework requires a Primary Key to generate a model from the database. If there is no Primary Key on a table it will simply select the non-nullable columns as a concatenated primary key and the Entity will be read/only."

Looking at your EAT_SourceNames object it appears there is no primary key field so the Entity Framework is using the column 'name' as part of the composite key which means it is read-only.

The solution would be to add a Primary Key field to EAT_SourceNames and then your 'name' field would no longer be part of the primary key.

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

The error message you are getting is accurate--you are setting the _truck.Id property, which by convention, is a primary key/identity field used by Entity Framework. You may want to simply add another field to your database, and to your data model if you are using EF Code First, to hold the txtplateNumber.Tag value. Either way, you are going to need to remove the code setting that value to _truck.Id.

The property is part of the object's key information and cannot be modified and I NEEDto modify it

Updating a primary key is considered very bad practice. If that value does need to change, then you should seriously consider whether that property should be used as a primary key. So you have two options:

  1. Stop refno being a primary key and add another one (e.g. an int Id property). You can however give the refno value a unique constraint.
  2. Instead of updating your entity, add a new one and delete the old one.

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

while creating table
u are mentioning

Id = c.Guid(nullable: false, identity: true),

so it will consider as auto generated field

Thats why it is giving this error when you try to assign it manually.
If you need to assign it manually modify your object class definition with this

[DatabaseGenerated(DatabaseGeneratedOption.None)]
public Guid Id { get; set; }

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

This happens because TryUpdateModel tries to update all posted values. So if you have one or more values that you don't want updated, you have to do this manually.

For instance:

TryUpdateModel(updateTopic,  "", null, new string[] { "Id" });

I'm guessing that one property named "ID" is also submitted into this action, causing the error.



Related Topics



Leave a reply



Submit