Skip to content

GUI context entities not updating with transaction context data updates, except new entities #36305

@WrithemTwine

Description

@WrithemTwine

Bug description

A WPF GUI application using DataGrid controls to visualize database tables. A long-running GUI context (and the bound Observable Collections) isn't updating data table records changed (and SaveChanges()) through short-lived transaction contexts. New records added through the transaction-context do appear in the GUI context.

Existing context for datagrids, new context per transaction -> new records will update in GUI; record content changes do not update in GUI.

------Background-------
I'm writing a chat bot for a popular streaming service using EFC backend database support. I've read through documentation and online sources for how can I implement a GUI real time update WPF app.

Found a "long running context" described here to bind observable collections to WPF DataGrid controls to display data table rows.
https://learn.microsoft.com/en-us/ef/core/get-started/wpf

Also, read about DbContext lifetime to see transactions should use a short-lived DbContext.
https://learn.microsoft.com/en-us/ef/core/dbcontext-configuration/

So, I initially started with EFC 8 with a single context and the GUI showed real time updates, binding observable collections ( context.DbSet.Local.ToObservableCollection() ). However, the GUI blocked with some of the larger transactions on the context. To provide a responsive GUI, I decided to split the single context to have a GUI Context to support the GUI datagrids, and short-term transaction contexts to perform most of the work. The end-user can/should-be-able-to update records in the GUI to edit strings or add new rows (such as a new chat bot command or to handle an overlay action -> popup a video), so the GUI context can't hold data with one-time binding and needs to report back user updates. Before converting, I did a small test and I did observe GUI datagrid updates in real-time (using notification events to refresh the datagrids). After I converted to use a GUI context and transaction-context, the application stopped updating data changes, sometimes showing new records.

transaction-context:
added/changed records
SaveChangesAsync( true ) - commit records to database

GUI-context:
DbSet.LoadAsync( ) - query table from database records, (should) get updated records
(notify datagrid and do an items refresh)

For testing some EFC code small scale, I've written a smaller project to do some concept testing before I code it all out in my major code. At a point in EFC 9, this project did have real-time updates. However, the latest EFC 9.0.6 update broke the real-time updates. I also observed, DbSets with fewer records (~30 records) updated changed records in real-time but larger DbSets (~500+ records) didn't seem to update.

Attached code: EFEntityEntryTesting.zip

----Concluding Thoughts----
I'm writing a WPF GUI app to visualize database data. I understand a long running DbContext is not preferred over short-lived single transaction contexts. A benefit of the two contexts allows heavy work to not block the GUI, and the GUI context only updates after the transaction. However, to rebuild the GUI context and re-bind the ObservableCollections to the data grids is very costly in the long run - even though a new GUI context would reflect the updated records. Sometimes, I'm wondering if there is data corruption with the outdated GUI context overwriting the newer transaction-context saved data, effectively reverting data to an earlier state (not tested). I'm only calling GUI context SaveChanges when the user touches the datagrid records.

This issue may not be a bug, but an intended behavior. I haven't found any definitive information about whether an existing context should properly update when a newer context performs and saves a transactional update. This app needs a real-time data visualization solution for GUI side I/O and an internet connection I/O from a streaming site - users aren't expected to be super tech savvy. Restarting the app (instantiating a new GUI context) refreshes the GUI with the new data.

I'm also wondering if too many requests in a short time are causing a kind of deadlock or collision in the task scheduler. I added some time delays to let tasks have a chance to complete. This didn't seem to make any difference with loading updated GUI context entities.

For WPF datagrid databinding, I use an intermediate class object to hold GUI properties and assign the context ObservableCollections to the intermediate class object properties for binding. The intermediate object also provides properties to compute some statistics of the datagrid records (e.g. total users, total followers, total default and user commands), hence need the intermediate properties. I've inspected GUI context loaded records during debug and they don't appear with updated data, so it doesn't appear to matter whether the datagrids are direct bind to ObservableCollections or referenced through the intermediate properties (object reference assignment, not a copy).

Thank you. Let me know if any more information is necessary.

Your code

EFEntityEntryTesting.zip (attached above) or https://github.com/WrithemTwine/TwineChatBot/tree/dev_EFC_Rewrite/EFCoreIssues

There might be a randomly occurring "concurrent operation" exception on a context from this simple project, but that isn't the focus of this project.

Upon starting, the GUI pops up and populates initial data. The following buttons are available:
(click first) "Start Stream" - simulate a new stream to test data updates, with a random selection of the existing users updated as present in this stream.
"Add Random Users" - randomly selects from the existing users to simulate users leaving and joining stream-the current group of users are now in the stream.
"End Stream" - stops the simulated live stream and boots out the users - all users are considered gone from stream with the "last seen" date ("End Stream" button pressed time).

During "Start Stream", the users added to the live stream should have real-time "Watch Time" data as rolling timespan data. The user also accrues "Currency" based on the "Last Seen" date to "now", and the "Last Seen" date updates through a currency accrual thread with a short delay between each loop. Users who leave stream no longer update currency.

The GUI should show the real-time data during a livestream and currently doesn't. The changed data will appear after restarting the app.

Stack traces

No exceptions.

Verbose output

N/A - not EF tools.

EF Core version

9.0.6

Database provider

Microsoft.EntityFrameworkCore.Sqlite

Target framework

.NET 9.0

Operating system

Windows 10 (dev & test), Windows 11 (production)

IDE

Visual Studio Community 2022 17.14.7

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy