实体框架 - 基础数据(在数据库中)更改通知 [英] Entity Framework - Underlying data (in database) change notification

查看:106
本文介绍了实体框架 - 基础数据(在数据库中)更改通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用实体框架来处理数据库中的数据,迄今为止已经取得成功。



然而,我想要使用多个应用程序数据在同一时间(并发版)。



有什么方法可以在数据库中的数据更改时收到通知?



我看到一个使用DML触发器的解决方案,但我想知道是否有其他方法来实现,如果是的话,最好的解决方案是使用。



问候,



Nic



编辑



也许我的问题不够清楚,我会尝试以一个例子来说明。




    <应用程序#1正在使用数据库#1上的实体框架
  • 应用程序#2还在数据库1上使用实体框架

  • 应用程序#1更改由数据库#1的基础表中的更改所反映的实体模型

  • 我希望应用程序#2获得noti这个变化可以有一致/ up2date数据。


解决方案

也许你应该考虑关于在您的应用程序中使用EF。 EF的上下文应尽可能短的时间使用:




  • 创建上下文

  • 加载数据

  • 修改数据

  • 保存数据

  • 删除上下文



    • 由于内部实现(IdentityMap,UnitOfWork)长寿命上下文不是一个不错的选择,并且在短暂的生活环境中,你根本不想提及行为。即使在桌面应用程序中,您应该使用像每个窗体的上下文方法您加载数据,您向用户提供数据,直到那时只有用户可以修改数据并按下保存按钮 - 应用程序负责处理并发问题(时间戳)。作为运行工作单元的一部分的数据的自动修改是非常糟糕的主意 - 如果用户已经修改过的数据怎么办?你会覆盖他的变化吗?



      编辑:



      ObjectContext 这里



      我可以想象需要数据更新通知客户端应用程序的场景。它可以是只读实时数据显示 - 例如股票交易信息。但在这种情况下,您需要更强大的东西。客户端调用ORM不是获取数据的场景,而是客户端订阅一些处理数据检索和快速更改通知的服务/中间层的场景。



      对于简单的场景您只需要以半实时的方式刷新数据,您可以使用轮询 - 您的客户端将在几秒钟内再次调用查询,并使用 StoreWins 策略。任何通知策略都在EF的范围之外 - 您必须将其实现为触发器,sql依赖关系,发布订阅模式或其他内容。即使有通知,您只能处理一些事件并重新查询数据。



      再次,如果要通过轮询减少数据传输,则需要一些服务/中间层,这将允许某种程度的缓存(您也可以尝试WCF数据服务) / p>

      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.

      解决方案

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

      这篇关于实体框架 - 基础数据(在数据库中)更改通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆