Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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's key information and cannot be modified.

Here is the code for the update function:

 if (context.EAT_SourceNames.Any(e => e.name == newSourceName))
 {
    MessageBox.Show("Name already exists in the Database");
 }
 else
 {
    var nameToUpdate = context.EAT_SourceNames.SingleOrDefault(e => e.name == sourceName.name);
    if (nameToUpdate != null)
    {
       nameToUpdate.name = newSourceName;
       context.SaveChanges();
       RefreshDGVs();
     }
 }

My SourceNames class looks like the following:

    public EAT_SourceNames()
    {
        this.EAT_Sources = new ObservableListSource<EAT_Sources>();
    }

    public string name { get; set; }
    public string version_id { get; set; }
    public string allocation_name { get; set; }

I searched for similar questions, but could not find any working solution.

like image 204
forgetaboutme Avatar asked Apr 14 '15 16:04

forgetaboutme


People also ask

Is part of the object's key information and Cannot be modified Entity Framework?

"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?

PersonnelCode' 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.


3 Answers

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.

like image 173
nvuono Avatar answered Nov 09 '22 02:11

nvuono


Same happened to me today. I set new entity's ID with the old record's ID and the error is gone.

//This checks whether there's a record with same specific data.
var kayitVarMi = _db.Sorgu.FirstOrDefault(a => a.Serial == sorgu.Serial);

                if (kayitVarMi != null) // If there's
                {
                    sorgu.Id = kayitVarMi.Id; //This one does the trick
                    _db.Entry(kayitVarMi).CurrentValues.SetValues(sorgu);
                }
                else // If not
                {
                    _db.Sorgu.Add(sorgu);
                }

                // This whole block is in a transaction scope so I just check recordability.
                if (_db.SaveChanges() > 0)
                {
                    _sorguKaydedildiMi = true;
                }
like image 40
Doğa Gençer Avatar answered Nov 09 '22 02:11

Doğa Gençer


Here is the proper solution of it.

You have to unchecked entity key of Name from your EAT_SourceNames.

You can do this by following steps.

  1. Open your .edmx file.
  2. Right click on your display and select Mapping Details.
  3. Click on Model Browser and you will find it at right side of your screen.
  4. Now In that go to Entity Types folder and click on your table in your case EAT_SourceName.
  5. Now you will find the model of EAT_SourceName , now right click on name and uncheck Entity Keys.
  6. Now Clean and Rebuild your Solution.
like image 4
Neel Darji Avatar answered Nov 09 '22 02:11

Neel Darji