I have a Microsoft Blazor Application which is using Entity Framework 6 to communication with PostgreSQL database using NPGSQL. I have the following scenario:
I have a button which enables a scheduler, and this button is updating a row on tableA that scheduler is enabled, column scheduled = true. Then another perioding thread is starting which is manipulating the same row on tableA to specify if the scheduler is on lock mode while ruining in order next iteration to skip, column lock = true.
I am facing some data update inconsistency as I see my first change of scheduler enablement, even after the SaveChanges with scheduled = true, in database is false. The sequence is the following Button schedule on -> thread start -> column schedule = true
I believe that this is because the ChangeTracker identifies a change either for schedule, either for lock and updating the whole record overriding the other change.
If this is true, is there any way to track into column level?
Found the solution for this problem,
First main thread which is manipulating the schedule status can have
_dbContext.Entry(mapping).Property(x => x.Scheduled).IsModified = true;
instead of
_dbContext.Entry(mapping).State = EntityState.Modified;
Second periodic thread which is manipulating the lock status can have
_dbContext.Entry(mapping).Property(x => x.Lock).IsModified = true;
instead of
_dbContext.Entry(mapping).State = EntityState.Modified;
This way, we can enforce the ChangeTracker to not consider other changes which may have been made before SaveChanges.