Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to update fetched data

I have written some code that gets loggings from a database, processes them and if something goes wrong, an error-message is written in the database. In the end of the code I remove all the loggings from the database where the error field (in the database) is null.

            var logs = ctx.Logs.Where(record => String.IsNullOrEmpty(record.Error))
                        .Where(record => !Beingprocessed.Contains(record))
                        .Take(100)
                        .ToArray();

The fetching/processing/deleting is done in multiple threads at once, so records can be removed by another thread from the database while this code is running. Is there a possibility to just update logs with the values from the database?

Something like

logs.Update();
ctx.Logs.RemoveRange(logs.Where(l => l.Error == null));
ctx.SaveChanges();

(I know I can just use the ID's from logs etc. to fetch them again but I want to know if there is anything that exists to update entities you fetched from database.)

like image 387
Alexander Derck Avatar asked Feb 01 '26 12:02

Alexander Derck


1 Answers

Maybe this will help:

ctx.Entry(Logs).Reload();
like image 104
Aleksa Avatar answered Feb 04 '26 02:02

Aleksa