17

I'm using the Entity Framework to manipulate data in a database with success so far.

However, I would like to have more than one application playing with the data at the same time (concurrent edition).

Is there a way to get notified when the data in the database changes?

I saw a solution using DML trigger, but I would want to know if there is other ways to achieve that and if yes, what is the best solution to do use.

Regards,

Nic

EDIT

Maybe my questions was not clear enough, I'll try to illustrate it by an example.

  • Application#1 is using entity framework on database#1
  • Application#2 is also using entity framework on database#1
  • Application#1 changes the entity model which is reflected by a change in the underlying table of the database#1
  • I want Application #2 to get notified of this change so it can have consistent/up2date data.
Nicolas
  • 1,464
  • 1
  • 17
  • 35

4 Answers4

5

Perhaps you should think about usage of EF in your application. EF's context should be used for as shortest period of time as possible:

  • Create context
  • Load data
  • Modify data
  • Save data
  • Drop context

Because of internal implementation (IdentityMap, UnitOfWork) long living context is not a good choice and with short living context you don't want mentioned behavior at all. Even in desktop application you should use approach like context per form. You load data, you present data to your user and till that time only user can modify data and push save button - it is application responsibility to deal with concurrency issues somehow (timestamp). Automatic modification of data which are part of running unit of work is pretty bad idea - what if user already modified data? Will you overwrite his changes?

Edit:

You can read more about impelementation of ObjectContext here.

I can imagine scenarios where data update notification to client applications are needed. It can be read-only real time data displaying - for example stock trading information. But in such case you need something more powerful. It is not scenario for client calling ORM to get data but the scenario for client subscribing to some service / middle tier which handles data retrieval and fast change notification.

For simple scenarios where you only need to refresh data in semi-real time fashion you can use polling - your client will call the query again within few seconds and use StoreWins strategy. Any notification strategy is outside scope of EF - you have to implement it as trigger, sql dependency, publish subscribe pattern or something else. Even with notification you will only be able to handle some event and requery the data.

Again if you want to reduce data transfer with polling you need some service / middle tier which will allow some level of caching (you can also try WCF Data Services).

Community
  • 1
  • 1
Ladislav Mrnka
  • 349,807
  • 56
  • 643
  • 654
  • 2
    This answer is completely off topic. Your answer of polling is exactly why the question is asked. To prevent polling. – Marc Johnston May 01 '15 at 21:23
2

At the database level, you can put a timestamp column on your table and use that to tell if a row has been updated since retrieval. This could be checked by a trigger as you suggest, or by a monitoring system in your C# code, or if you just want to prevent overwriting changes, you can write update sprocs that check the timestamps, and use those to save your entities.

No matter which option you choose, you'll have to decide ahead of time how you want to manage conflicts.

Timestamp-based concurrency control

Justin Morgan
  • 27,557
  • 11
  • 71
  • 100
  • 1
    +1 for the best approach here. You HAVE to use timestamp, that is the best approach for concurrency checks and now EF 4.2 does support that automatically. – Anderson Matos Feb 07 '12 at 12:18
0

You could use database cache dependency for things like this.

http://msdn.microsoft.com/en-us/library/system.web.caching.sqlcachedependency.aspx
http://msdn.microsoft.com/en-us/magazine/cc188758.aspx

Although it's going to be a bit more complicated when you have a web application compared to a desktop/service application.

Robert Koritnik
  • 97,460
  • 50
  • 267
  • 388
0

I don't know what provider you use. Anyway both Devart and ODP support DB Notification. For Devart you can see the following link OracleDependency Class

Daniel Kabzon
  • 326
  • 2
  • 17