I am using Sql Server on Linux with EC Core (2.1.0-preview1-final)
I am trying to update some data coming from a web Api service (PUT) request. The data (ticket) is passed correctly and gets deserialised into an object (ticket).
When I try to update, I use the following code:
public Ticket UpdateTicket(Ticket ticket) { using (var ctx = new SupportTicketContext(_connectionString)) { ctx.Entry(ticket).State = EntityState.Modified; ctx.SaveChanges(); // <== **BLOWS UP HERE** var result = ctx.Tickets .First(t => t.TicketId == ticket.TicketId); return result; } }
The code throws the following error:
Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded
I have tried a number of modifications to the code, including
ctx.Attach(ticket);
but this does not help.
How do I get it to update the database successfully? Any ideas on how to debug this? Logging seems to be difficult to set up.
Any ideas greatly appreciated.
The Entity Framework Core Fluent API ValueGeneratedNever provides a way to specify that the value for the selected property should never be generated automtically by the database. This is useful if you want to circumvent the database's default behaviour.
For me, there were two silly errors I made. First, I tried to call the Update() method without specifying a DbSet:
_applicationDbContext.Update(user);
Correct way:
_applicationDbContext.Users.Update(user);
The second, equally silly, error, was trying to update a model that without first pulling it from the database:
public bool UpdateUser(IdentityUser model) { _applicationDbContext.Users.Update(model); _applicationDbContext.SaveChanges(); return true; }
Nope.
I first needed to retrieve it from the database, then update it:
public bool UpdateUser(IdentityUser model) { var user = _applicationDbContext.Users.FirstOrDefault(u => u.Id == model.Id); user.PhoneNumberConfirmed = model.PhoneNumberConfirmed; _applicationDbContext.Users.Update(user); _applicationDbContext.SaveChanges(); return true; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With