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
Related
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
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.
I need to know if a new record has been added to a particular table in my windows application. Table might be manipulated by different applications.
For now I'm using a Timer control and querying to see if there is a new record (I add record content somewhere in my application and delete record to avoid getting duplicate record), but of course this is not a clean way for this purpose.
Is there something like an event or something better than my approach?
I'm using entity framework 6
Update: I've read about SqlDependency but I don't know if it can be implemented using entity framework
You can give Linq2Cache a try to leverage SqlDependency, last time I hear EF6 was cleaned up its act and now formulates queries which are Query Notifications compatible.
Here's an existing StackOverflow article about that using the SQLDependency class.
Alternatively, you can cache the last record ID in memory/local to your program (in a file) or write the last processed record ID to a database table and search for anything more recent than that.
This might seem like an odd question, but it's been bugging me for a while now. Given that i'm not a hugely experienced programmer, and i'm the sole application/c# developer in the company, I felt the need to sanity check this with you guys.
We have created an application that handles shipping information internally within our company, this application works with a central DB at our IT office.
We've recently switch DB from mysql to mssql and during the transition we decided to forgo the webservices previously used and connect directly to the DB using Application Role, for added security we only allow access to Store Procedures and all CRUD operations are handle via these.
However we currently have stored procedures for updating every field in one of our objects, which is quite a few stored procedures, and as such quite a bit of work on the client for the DataRepository (needing separate code to call the procedure and pass the right params for each procedure).
So i'm thinking, would it be better to simply update the entire object (in this case, an object represents a table, for example shipments) given that a lot of that data would be change one field at a time after initial insert, and that we are trying to keep the network usage down, as some of the clients will run with limited internet.
Whats the standard practice for this kind of thing? or is there a method that I've overlooked?
I would say that updating all the columns for the entire row is a much more common practice.
If you have a proc for each field, and you change multiple fields in one update, you will have to wrap all the stored procedure calls into a single transaction to avoid the database getting into an inconsistent state. You also have to detect which field changed (which means you need to compare the old row to the new row).
Look into using an Object Relational Mapper (ORM) like Entity Framework for these kinds of operations. You will find that there is not general consensus on whether ORMs are a great solution for all data access needs, but it's hard to argue that they solve the problem of CRUD pretty comprehensively.
Connecting directly to the DB over the internet isn't something I'd switch to in a hurry.
"we decided to forgo the webservices previously used and connect directly to the DB"
What made you decide this?
If you are intent on this model, then a single SPROC to update an entire row would be advantageous over one per column. I have a similar application which uses SPROCs in this way, however the data from the client comes in via XML, then a middleware application on our server end deals with updating the DB.
The standard practice is not to connect to DB over the internet.
Even for small app, this should be the overall model:
Client app -> over internet -> server-side app (WCF WebService) -> LAN/localhost -> SQL
DB
Benefits:
your client app would not even know that you have switched DB implementations.
It would not know anything about DB security, etc.
you, as a programmer, would not be thinking in terms of "rows" and "columns" on client side. Those would be objects and fields.
you would be able to use different protocols: send only single field updates between client app and server app, but update entire rows between server app and DB.
Now, given your situation, updating entire row (the entire object) is definitely more of a standard practice than updating a single column.
It's better to only update what you change if you know what you change (if using an ORM like entity Framework for example), but if you're going down the stored proc route then yes definately update everything in a row at once that's way granular enough.
You should take the switch as an oportunity to change over to LINQ to entities however if you're already in a big change and ditch stored procedures in the process whenever possible
I haven't been able to find anything that addresses my problem. Here's the scenario. We have an application that generates lists of entities in memory (List) when the application starts. As long as data manipulation is done from within this application, changes are persisted to the database and back to the client application just fine. However, we've encountered a case where the database record that an entity is mapped to in memory of the application could be modified elsewhere outside of the application. These changes aren't seen until the application terminates and is restarted. Could somebody point me in the right direction on how to take an existing entity (or list of entities) and "refresh" the property values with any possible changed values in the underlying database record that it is mapped to?
Entity Framework has a "Refresh" method built into it (see MSDN article here). I think the refresh mode you would want is "Store wins". This will reset all your values in your application with what is currently stored in the DB.