Change notice for underlying data (in database) in Entity Framework

c#-4.0 entity-framework notifications


I've had success so far utilizing the Entity Framework to modify data in a database.

However, I would want to have many applications interacting with the data at once (concurrent edition).

Is it possible to be informed when the information in the database changes?

I found a solution utilizing a DML trigger, but I'd want to know if there are any other options and, if so, which one is the best.




Perhaps my query wasn't clear enough, so let me give you an example.

  • Entity Framework is being used by Application #1 on Database #1.
  • On database #1, application#2 also utilizes the entity framework.
  • Application #1 modifies the entity model, which is mirrored in the database #1's underlying table.
  • In order for Application #2 to have consistent/up-to-date data, I want it to be informed of this change.
1/31/2011 3:51:08 PM

Accepted Answer

You may want to consider using EF in your application. The smallest time feasible should be spent in the EF context:

  • Establish context
  • Insert data
  • alter data
  • data backup
  • Remove context

Long lasting context is not a suitable option due to internal implementation (IdentityMap, UnitOfWork), and with short living context you don't want described behavior at all. Even in desktop applications, you need to utilize a context-per-form approach. You load data, give it to your user, and until that point, only the user may edit the data and press the save button. It is the job of the program to address concurrency concerns in some way (timestamp). Automatic data alteration is not a good concept because what if the user has already changed the data? Will you revoke his modifications?


More information on the implementation ofObjectContext here.

I can think of situations when client apps need to be notified when data changes. Real-time data may be shown that can only be read, such as stock trading information. However, in this situation, you need something more potent. It is not the case that a client calls an ORM to get data; rather, it is the case that a client subscribes to a service or middle tier that deals with data retrieval and quick change notification.

Polling may be used in basic instances when you simply need to update data in a semi-real time manner; your client will call the query once more after a short period of time and employ the StoreWins technique. Any notification technique must be implemented as a trigger, sql dependence, publish-subscribe pattern, or another method as it is beyond the scope of EF. You will only be able to respond to specific events and requery the data, even with notice.

Once again, if you want to use polling to limit data transmission, you'll need a service or intermediate tier that permits some amount of caching (you can also try WCF Data Services).

5/23/2017 12:08:50 PM

Popular Answer

You may add a query at the database level.timestamp Use a column in your table to indicate if a row has been modified since retrieval. If you only want to avoid overwriting changes, you may build update sprocs that check the timestamps and use them to save your entities. This might be verified by a trigger as you mention, by a monitoring system in your C# code, or if you just want to prevent overwriting changes.

You must determine how you wish to handle disagreements in advance regardless of the option you choose.

Control of concurrency using timestamps

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow