EfCoreInAction icon indicating copy to clipboard operation
EfCoreInAction copied to clipboard

Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException

Open EngMohammadAbdullah opened this issue 6 years ago • 5 comments

Hey Jon I am getting this Exception when I am trying to update ICollection<Reviews>

'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 many solutions . but not worked !!

could you please help me ?

EngMohammadAbdullah avatar May 26 '19 12:05 EngMohammadAbdullah

I could use update in a dummy way !! First: I cleared the items from order, then _context.Items.add( new Item { OrderKey = order.Key, Color = "Blue", width = 240d }) then _context.SaveChanges(); But it is not an efficient way !! –

EngMohammadAbdullah avatar May 26 '19 12:05 EngMohammadAbdullah

Hi @EngMohammadAbdullah,

I'm not sure what your problem is, but its something to do with the Review you were trying to update isn't in the database. This happens if EF Core can't find the primary key of the Review are trying to update - did you miss setting the ReviewId property in the Review class?

JonPSmith avatar May 28 '19 13:05 JonPSmith

Thanks a lot for tour replay , your book is a real treasure :) Actually I have checked many times the Item object and the order object , they both have key

public class Item : EntityBase {

    public string Name { get; set; }
    public double Height { get; set; }
    public double width { get; set; }
    public string Color { get; set; }
    public Guid OrderKey { get; set; }

    public Order CustomerOrder { get; set; }
}`

` public class EntityBase : IEntity { public EntityBase() { Key = Guid.NewGuid(); }

    [Key]
    public Guid Key { get; set; }

}`

`

When you try to remove the Items it works and save changes , but when you try to update Items , it throws this exception !! I have made a screenshot for the variables . Untitled

EngMohammadAbdullah avatar May 28 '19 13:05 EngMohammadAbdullah

Hi @EngMohammadAbdullah,

I'm still not sure what is going on as there are lots of classes involved. The bit that looks a little odd is you are creating a new list of updatedOrder.Items, which would throw away the existing Items and replace them with your new Items. I would have expected you to edit the loaded items instead, something like this

updatedOrder.Items[0].Color = "Red";
updatedOrder.Items[1].Color = "Blue";

Your "delete old and replace" can work but the Item keys MUST be copied over, and I can't tell if you are doing that (can't see the code underneath the values).

JonPSmith avatar May 29 '19 10:05 JonPSmith

I appreciate your time , I have searched a lot for this bug ,and it can save a lot of times , because when I need to update such an entity I do a lot of steps , I take the id from the "Dependent entity" here Order end modify the "Principal entity" here : Items I have created a repository in my Account . could you please take a look at Program class in a Console Project

https://github.com/EngMohammadAbdullah/JonPSmithProject

thanks a lot

EngMohammadAbdullah avatar May 30 '19 17:05 EngMohammadAbdullah