Hi,
it would be a great feature in EF6 to automatically detect and refresh data changes. I'm using DbContext and DbSet.
A very good approach is described in Harry von Borstel's article
"AutoRefresh Entity Framework Data Using SQL Server Service Broker"
http://www.codeproject.com/Articles/233770/AutoRefresh-Entity-Framework-data-using-SQL-Server
There are two problems with that solution:
I tried another approach using SqlDependency and a DataTable to track changes. Unfortunately a second connection is needed and every monitored table needs a second query.
Regards
Mathias
it would be a great feature in EF6 to automatically detect and refresh data changes. I'm using DbContext and DbSet.
A very good approach is described in Harry von Borstel's article
"AutoRefresh Entity Framework Data Using SQL Server Service Broker"
http://www.codeproject.com/Articles/233770/AutoRefresh-Entity-Framework-data-using-SQL-Server
There are two problems with that solution:
-
Sometimes the refresh triggered by the CallContext is faster than then the reread of the changed entities. When using timestamps to prevent unintended overwrites of data SaveChanges results in an exception:
System.InvalidOperationException
HResult=0x80131509, COR_E_INVALIDOPERATION
The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges. -
The need of the AutoRefreshWrapper class to handle the CollectionChanged event. It is required, because the event is fired in another tread and must be dispatched to the main thread:
System.Windows.Application.Current.Dispatcher.BeginInvoke((Action)(notifyRefresh.OnRefresh));
That could be avoided with a delegate.I tried another approach using SqlDependency and a DataTable to track changes. Unfortunately a second connection is needed and every monitored table needs a second query.
Regards
Mathias