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.)
Maybe this will help:
ctx.Entry(Logs).Reload();
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