1

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.

5
  • 2
    What have you actually changed there? I can see you're getting a record from the db, but not doing anything with it and then calling SaveChanges() ? Commented Aug 25, 2015 at 19:10
  • 1
    Your code is pretty limited here, there's lots of possible issues. Did you try using the SQL profiler to see if any SQL is sent to your DB? Commented Aug 25, 2015 at 19:10
  • What's the error you get? Commented Aug 25, 2015 at 19:13
  • There is no modified data in newsToUpdate. As far as we can tell from this sample it's possible it is saving to the database but has no new values in the original object. Commented Aug 25, 2015 at 19:14
  • Ok, so I have a model tblTips tips in which that is getting a few fields updated, but i see that I am not wanting it to update all the fields and it is trying to update a field that is "not null" with a null , so I'm writing this code and I hope that this is not "bad to do. db.Entry(tips).State = EntityState.Modified; db.Entry(tips).Property(x => x.createdby).IsModified = false; Commented Aug 25, 2015 at 19:25

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I typically always use TFS or Git, I happen to have been doing this as a POC in which I know as weeks go on i'm putting myself in a bad spot without revisions.
@BrianThornton: Regarding your update, I think if you do the db.Entry() stuff to newsToUpdate instead of tips, that might work. Alternatively, you could just manually map each value from tips to the newsToUpdate's properties--more tedious, but also less likely to bite you when your model changes and users can update values you didn't want them to have access to.
@BrianThornton: OR, if you were intentionally trying to apply those changes via tips and you don't actually need newsToUpdate, comment out that line that pulls newsToUpdate() from the database. The problem you're running into is that EF sees two objects that claim to represent the same object in the database.
1

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();

5 Comments

Oh, so I can literally always use var recordsUpdated = db.SaveChanges(); to get more insight into what happened?
Is it "ok" to specify ignoring columns to update from my model with this code? db.Entry(tips).State = EntityState.Modified; db.Entry(tips).Property(x => x.createdby).IsModified = false;
I was just giving an example of how you can check if anything has changed, if you know you're getting back 0, then you know there's something up in the code.
@BrianThornton, are you looking to AddNews or update a news item?
Update a News item which happens to be the model tblTips , thus tips is containing the data , but then I have this newsToUpdate which I gather is the data from my model hydrated from the database from .Find(id)
0

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.

2 Comments

I should have posted my model tblTips is the model and it contains the fields
Hi, I get an error when attempting to only update specific columns
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.