I have code which I thought was working for Entity Framework update sql server table. I do not get an error upon stepping through the code.
var newsToUpdate = db.tblTips.Find(id);
//if(TryUpdateModel(newsToUpdate, "",))
try
{
db.SaveChanges();
}
catch (RetryLimitExceededException)
{
ModelState.AddModelError("", "unable to save changes");
}
- Notice I have that
if(TryUpdateModel(...line I don't recall if I had used that when records in the database table DID update. - I can see that the Model has the correct
id - DB table is not updated.
What can I do to figure out WHY in C# visual studio as to it not updating the record? There are no errors.
UPDATE:
my model is tblTips , signature on my method is
public ActionResult AddNews(tblTips tips, int? groupid, int? id)
Seems that the update page is not knowing about other columns and trying to update all the columns of which it is trying to update a column to null in which in the db table that column is a "not null"
Is it "ok" ( yes yes i know i should use a viewmodel) to do the following to exclude a column from the update?
db.Entry(tips).State = EntityState.Modified;
db.Entry(tips).Property(x => x.createdby).IsModified = false;
db.SaveChanges();
I guess I should not believe everything I read on Stackoverflow , that failed above with
Attaching an entity of type 'BRM.Data.Models.tblTips' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.
SaveChanges()?