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

后端 未结 6 1702
无人共我
无人共我 2020-11-29 11:00

I am trying to update a record and I get this error message after the context.SaveChanges();

The property \'name\' is part of the object\

6条回答
  •  無奈伤痛
    2020-11-29 11:33

    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.

提交回复
热议问题