Let me explain the title.
I'm using Entity Framework Code First in an application (TestApp). For debugging purposes the TestApp connects to an SQLExpress database (central database server).
To keep things simple here, the database contains one table "Products" and the TestApp shows all "Products" from the database in a datagrid and TestApp can add/delete a "Product" or modify the ProductName.
public class Product {
public int ProductId { get; set; }
public string ProductName { get; set; }
}
public DbSet<Product> Products { get; set; }
I have for example 3 pc's where TestApp is installed and running (I'll call them Client_X). If I add a new "Product" via Client_1, than this is not directly visible in the TestApp on Client_2 and Client_3. Only when Client_2 and Client_3 fetch all the data again (manual refresh) than I see the newly added record.
What I want to know: How can I be aware of changes in the database via EF Code First? How would Client_2 and Client_3 get their datagrid automatically updated because a new item was added or removed?
I'm not sure if it is asked too much but a simple sample project or simple code to demonstrate this would be useful.
FYI: I'm fairly new to EF Code First. (I'm using .NET 4.0 and EF Code First 4.3.1)
Thanks in advance.
Scheme:
Client_3<
\
\
\
\
\>
Client_1 <---> [Central Database] <----> Client_2
Update/Edit:
Ok, it is clear that the Entity Framework itself doesn't do any syncing between multiple clients.
So i'll ask another question: I'm thinking of working the Client-Server way:
- A single server application which creates the database and uses the Entity Framework code first.
Multiple clients that communicate with that single server application.
What would now be the best way to make sure the clients use the same data and see eachothers updates/changes? (I would appreciate code samples.)
This is not the responsibility of EF, it is more down to your general application design. A relatively simple background or periodic polling process would solve your problem.
There are more considerations when dealing with multiple users, such as record locking and it may be an idea to do a little research into this side of things and come back if and when you have a specific problem.
There is nothing you can use to notify clients using entity framework. You can write an overload for
SaveChanges
, or create extension method but as far as you do not have client->server architecture I can not imagine how you can achieve this.If you use WCF as your gateway to the database, you could create duplex communication between your client and server and as new records are available clients would be notified using Callback Channel.
Another approach is to use MS SQL Service Broker Overview (Service Broker)
Despite the fact, that there are many different ways to get desired result, none of them are related to entity framework as it is ORM nor a application framework or Communication framework for applications built with .NET.