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

c# entity-framework

Accepted Answer

ZZZ_tmp
35
5/23/2017 12:02:17 PM

Popular Answer

I experienced the same today. The error has vanished once I set the new entity's ID to the old record's ID.

//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;
                }


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow