简体   繁体   中英

Entity Framework is not updating my table

I'm trying to do a simple update using Entity Framework. No matter what I try, this will not save my changes in the database. No errors are thrown:

using (var ctx = new DatabaseContext())
                {
                    var orders = ctx.Order.Where(o => o.Status == 1).ToList();
                    foreach(var o in orders)
                    {
                        o.Status = 2;
                    }
                    ctx.Entry(orders).State = EntityState.Modified;
                    ctx.SaveChanges();
                }

I have verified that orders has the data I expect and that it is updated as I expect before saving changes. And that my context is pointing to the correct database.

I have tried removing ToList(), adding the Entry.State line, and generally moving and tweaking the code. Nothing works.

Looking over Microsoft's documentation, my code matches what the example: https://learn.microsoft.com/en-us/ef/core/saving/basic

This should be very simple and I can't for the life of me figure out why it isn't updating my table. Any ideas?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM