Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

c# 4.0 - Entity Framework - Underlying data (in database) change notification

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.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...