How can I find changes in the database using Sync framework 2.1?
what is try to accomplish is:
I have different scopes in the database. When a user inserts or updates something in the application database(local), the application needs to sync with the server database. Is there a way to let Sync framework only sync the tables with changes? That will be a lot more efficient than this:
foreach (string scope in _scopenames)
{
StartSync(scope);
}
I can't just send the parameter with the tablename from the model class to the sync class because in that way only one table will sync. and you will not recieve the changes from other clients.
regardless of how many tables you have in a scope, only the tables that has changes cause a sync. if you want to be able to control specific tables only to sync, then you can create one scope per table.
Related
i am currently developing a multi user wpf application.
First of all i wanna give you a short overview about the system.
Each client loads the full data from a sql server database. Each client can create and modify data which belongs to him. Data from other clients cannot be modified.
What i wanna do now is, sync my (long living -> context lives as long as the MainWindow is running) dbcontext, means when other clients write / update data in the database this data should be synced to each dbcontext, means each client can see the data of all other clients but is not allowed to modify / delete them (each record contains user field)
Now the big question is, how can i achieve the sync of the long-living dbcontext?
Is there any kind of sync framework (i have found the microsoft sync framework, but i think this solution does not fit for my problem.) I am looking for a method to sync the full dbcontext (all entities). Do i need to write my own sync, like polling the db?
Ok, i found a way to refresh all entities that were loaded first within the dbcontext using 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);
Now this only refreshes already existing entities. No the other thing is,
how can i get all new added records in the database and add them to my context.
Is there a way to reexecute my linq query and merge the results?
I am using Entityframework code first approach.
Thanks,
Mani
I have a database which is created in a separate project and a .edmx model file is generated by Entity Framework and created the model classes from the existing database.
There are several things that are added to the database (other parts of the backend, front end site, api, etc). Currently the method I have is a loop that checks for new entries in the database every 5 seconds (basically just a call to the table that looks for entries newer than the most recent entry I know of), and then I use the entry to perform actions that are non database related.
I was wondering if there was a better way to get new entries as opposed to constantly querying the database for something new. I was wondering if what I'm doing is fine, or if there's a better way to get new entries, preferably able to be built upon/with EF.
Thanks for any help!
If you want to notify your app as soon as any database records are inserted or updated or deleted and do some extra processing on them then you have two choices.
You can go with SqlDependency or SqlTableDependency. Both are used to notify the application when something on database changes. There is just one constraint where you must be able to enable the Broker for SQL server using ALTER DATABASE MyDatabase SET ENABLE_BROKER (This is important as some db doesn't support broker services i.e SQL Azure )
Here are some good links to explore both the approaches.
https://github.com/christiandelbianco/monitor-table-change-with-sqltabledependency
https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql/detecting-changes-with-sqldependency
So I have an application that uses Entity Framework for its data, but now I've hit a wall I haven't figured out a way over.
So the basic use-case is that I have a set of data that is modified quite frequently and shown to every user connected at the time. Changes made on the client are not instantly saved to the database but only once the user commits the changes. Now, is it possible to build a system that informs the other users of the modifications made to the database and have them automatically refresh their context? I really wouldn't want to create a polling system of any kind, but instead receive events or something of the sort. Also the absolute best thing would be to allow for combining of the changes (say I'm editing itemA, another user edits itemB and commits the changes, I get an event and only update itemB still keeping the changes I've made thus far to itemA).
Possible version for use are EF 4.3.1 ObjectContext-based, or EF 5.0.0 DbContext-based versions. Used database is MS SQL.
My situation is as follows. I'm using entity framework (4.0) in which I have defined a relatively simple model like, let's say, two entities:
user
transaction
A user can have multiple transactions in this scenario. When generating the database this would result (obviously) in 2 database tables names 'user' and 'transaction'.
The problem is that I want to use the code as a webservice where different companies should have their own environment (read: own users and transactions). A simple solution would be to add a column to both tables like, lets say 'company_id' to identify one user/transactions for companya from the user/transaction from companyb.
What I would like to have is a duplication of the tables like so:
compa_user
compa_transaction
compb_user
compb_transaction
This way..all data would be nicely separated and if company a generates a lot of transactions, company b would not notice that the system is getting slow or whatsoever.
My question: is there a way to accomplish this based on entity framework. So, can I have multiple tables representing one entity in my model and switch from table to table depending on which company is connecting to the service.
Any help appreciated!
If you really want to keep the tables seperate, then a seperate database for each client would be the easiest - only need to change the connection string in EF.
The other benefit of this model (seperate databases) is that the database will scale quite easily as each database could theoretically be on a different database server should the DB ever become the bottleneck.
can microsoft sync framework sync database views ?
or in your SelectIncrementalInsertsCommand can you select and join to multiple tables kinda doing something like a view?
Sync Framework does not support Views. You can changes the SelectIncrementalInsertsCommand to return data, however you need to make sure it gives back exact schema expected, no extra columns no mis matched data types. There is another way if your scenario really demands extra info. You can manipualate the data set retrieved in the ChangesSelected event of Provider. However you need to be careful about performance hit here.
Views are created dynamically on execution s you cant sync them.
BUT you can:
aggregate the view's data into a table and then sync the table.
Run the view on the synced tables on the target DB.