user5154099
user5154099

Reputation:

Entity Framework update is not updating database table

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");
}
  1. Notice I have that if(TryUpdateModel(... line I don't recall if I had used that when records in the database table DID update.
  2. I can see that the Model has the correct id
  3. 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.

Upvotes: 1

Views: 10956

Answers (4)

Alexius The Coder
Alexius The Coder

Reputation: 103

In my case, it was the lack of a Primary Key in the table model. After explicitly specifying a [Key] attribute for ID column, the values in the database started updating

[Key]
[Column("ID", TypeName = "NUMBER")]
public decimal Id { get; set; }

Upvotes: 0

Christian Phillips
Christian Phillips

Reputation: 18769

If you look at the documentation for DbContext.SaveChanges, you'll see it mentions the following...

Saves all changes made in this context to the underlying database.

Since you've made no changes (in the shown code), the expected result is happening.

Also, the api public virtual int SaveChanges() suggests you can get the number of records updated from SaveChanges, e.g.

var recordsUpdated = db.SaveChanges();

Upvotes: 1

StriplingWarrior
StriplingWarrior

Reputation: 156654

As it stands, your code now says "get this thing out of the database, then save any changes I just made", but it never actually changes the thing you got out of the database.

Most likely your problem came when you commented out the call to TryUpdateModel(), which ASP.NET MVC uses to make modifications to the given object based on parameters passed into the HTTP request.

I highly recommend using a version control system to make it easier to see what you've changed at any given point in time, and undo things you changed by mistake.

Upvotes: 1

Fyodor Soikin
Fyodor Soikin

Reputation: 80805

In the code as you posted it, there are no changes made to any data. Entity Framework correctly determines this and does not issue any update statements.

It appears that the TryUpdateModel call is what makes changes, though I wouldn't be able to tell without looking at its code. Since this call is commented out, the changes aren't happening.

Upvotes: 0

Related Questions