I'm having an exception that says The property 'itemId' is part of the object's key information and cannot be modified. This happened when I changed my raw sql query into entity framework. Here is the code for the raw sql query, which is working by the way:
con1.Open();
cmd1.CommandText = "UPDATE [stringInstrumentItem] SET [itemId] = @newId WHERE itemId = @oldId";
cmd1.Connection = con1;
cmd1.Parameters.Add(new SqlParameter("@newId", id));
cmd1.Parameters.Add(new SqlParameter("@oldId", plusId));
a = cmd1.ExecuteNonQuery();
con1.Close();
cmd1.Parameters.Clear();
And then, I changed that code into entity framework:
using (var context = new MusicStoreDBEntities())
{
var bay = (from s in context.stringInstrumentItems where s.itemId == plusId select s).FirstOrDefault();
bay.itemId = id;
context.SaveChanges();
}
It says that I cannot modify the itemId. How come I can modify itemId using the raw sql query version while the entity framework version doesn't? I want to know what is wrong with the entity framework version and How to solve this?