Reputation:
I am currently developing a multi user data management system in .NET 4.0.
My frontend is a WPF application which retrieves all the data from a SqlExpress Server. I am using the latest Entityframework version as main datalayer to read and write to the database.
The software has to handle following scenario. Each Frontend (WPF Application) can add and modify data in the database which is created by the client hisself. The client is not allowed to modify / delete records from other users therefore each Entity has stored the username information on the main entity.
Here you can see the datamodel I am using (EF Code First Approach).
I am using this Query to load all the data into the frontend application:
public static Context GetDataContext()
{
return new Context(Settings.ServerConnection);
}
public Project LoadProject(int id)
{
Remoting.Context = GetDataContext();
Project = Remoting.Context.dbsProjects.Where(p => p.Id == id)
.Include(p => p.Categories.Select(x => x.Templates.Select(y=>y.Properties.Select(xx=>xx.Binding))))
.Include(p => p.Categories.Select(x => x.Templates.Select(y => y.Links)))
.Include(p => p.DataSources).FirstOrDefault();
OnPropertyChanged("Project");
return Project;
}
After that initial load all data is displayed correctly in my frontend. So far so good that was the easy part.
Now I want to synchronize my DbContext, which is a long living context, lives for the complete runtime of the MainWindow. Means when another frontend inserts updates data which belongs to him, I want to display it also in all the other clients.
I have found a solution how to refresh all already existing entities within one context, therefore I use this lines of code:
var refreshableObjects = Remoting.Context.ChangeTracker.Entries().Select(c => c.Entity).ToList();
Remoting.Context.ObjectContext.Refresh(System.Data.Entity.Core.Objects.RefreshMode.StoreWins, refreshableObjects);
I am not sure if this is the right solution, but so far it works for me.
So far i can refresh all already loaded entities, but i have no idea how i can detect entities which are created or deleted by another frontend which is running at the same time. Is there any build in mechanism to refresh the initial linq query that loads or removes entities which are new in the database?
Thanks for your help!
Upvotes: 3
Views: 2228
Reputation: 7301
I solved this issue years ago by using Service Broker
. I have done a little research about this issue about how this is solved nowadays and it seems that it is done the same way. Searching on GitHub I found the following project that should suit your needs:
monitor-table-change-with-sqltabledependency
Using this library you can listen on the SqlTableDependency.Change
-event and get notified when entries on a table are updated, inserted, deleted.strong text
Upvotes: 3