16

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.

ocuenca
  • 38,548
  • 11
  • 89
  • 102
forgetaboutme
  • 613
  • 1
  • 6
  • 18
  • Dit you generate your model from the database or vice-versa (database-first or code-first)? Is `name` a primary key on the database table? Are you trying to update the original record or copy to a new one with a new key? – D Stanley Apr 14 '15 at 16:31
  • I doubt there is a "solution". Keys are used in the database to connect tables together, so you're normally prevented from modifying records that are part of the key unless you do so in both tables. We can't tell the relationships that your table might have, because you haven't posted that information. – Bob Tway Apr 14 '15 at 16:31
  • 2
    Is there a PK on the table at all? If not, EF uses all of the fields/columns as part of the "key information." – Saagar Elias Jacky Apr 14 '15 at 16:33
  • 1
    Note that there is an intermediate step between explicitly declaring an ID and using all required columns as ID: if you have a field "Id" or of form "Id" (so SourceNamesId), it will use that as a single PK instead.
    – Jeroen Vannevel Apr 14 '15 at 16:43

6 Answers6

36

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.

Community
  • 1
  • 1
nvuono
  • 3,323
  • 26
  • 27
10

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;
                }
Doğa Gençer
  • 124
  • 3
  • 15
4

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.
Neel Darji
  • 143
  • 3
  • 15
3

The only way I can think to update a text primary key is by using the following.

I do not believe it is best practice to use a "functional" primary key. A primary key's purpose is simply to uniquely identify a row.

context.Database.ExecuteSqlCommand("UPDATE Table SET [Name] = {0} WHERE [Name] = {1}", nameProperty, oldNameProperty);
Cyberdrew
  • 1,832
  • 1
  • 19
  • 39
2

Probably name is a part or full Primary Key for your EAT_SourceNames entity. You cannot modify object's PK, is it EF's limitation (see this thread).

Community
  • 1
  • 1
Yura
  • 2,013
  • 19
  • 25
0

The point is that you work with an object. The "name" property identifies the object, that's why you can't modify it. The solution is to modify the value in the table with a SQL statement (UPDATE) and reload the context. Sincerely.