我尝试将 EF4 与 SQLDependency 对象一起使用,但运气不佳。
我创建了自己的队列和服务,并希望我的 WPF 应用程序监视此队列的数据更改,以便我可以更新 UI(选择尝试此操作而不是不断查询数据库)。
即使在应用程序启动时我调用;
SqlDependency.Start(connectString, "NewResultAddedQueue");
我的 EF4 存储库实现抛出异常;
{"当使用 SqlDependency 时没有
提供期权价值,
必须调用 SqlDependency.Start()
在执行命令之前添加
到 SqlDependency 实例。"}
无法实现的目标,或者是否可以采取另一种方法来允许我的应用程序侦听 SQL 2005 中的数据更改?
I am trying to use EF4 with the SQLDependency object with not much luck.
I have created my own queue and service and want my WPF application to monitor this queue for data changes so that I can update the UI (opted for trying this rather than constantly querying the database).
Even though on application startup I call;
SqlDependency.Start(connectString, "NewResultAddedQueue");
My EF4 repository implementation throws an exception stating;
{"When using SqlDependency without
providing an options value,
SqlDependency.Start() must be called
prior to execution of a command added
to the SqlDependency instance."}
Firstly, am I trying to achieve something which is not possible with EF4 or is there another approach I can take to allow my application to listen for data changes from SQL 2005??
发布评论
评论(1)
首先,如果运行多个 WPF 应用程序实例,则无法使用自己的队列和服务。 SqlDependency 基础设施按应用程序域运行,如果您共享队列,则实例(应用程序域)将窃取彼此的通知,从而导致运行时混乱。除非您确切知道自己在做什么,否则只需使用默认实现(即时部署的服务/队列/过程)。
错误消息非常清楚:“当使用 SqlDependency 而不提供选项值 yada yada yada”。因此,您不提供
options
值 ,因此使用默认值:同时,您将覆盖
.Start()
调用中的默认服务。正如我所说,最好先让默认情况正常工作,直到您确定自己了解发生了什么,然后再冒险进入非默认自定义行为,特别是像 神秘通知。请查看 LinqToCache,了解如何将查询通知与 LINQ 结合使用。文章基于 SqlDependency 的 LINQ 查询缓存解释了此集成如何与 LinqToSQL 和 LinqToEF 配合使用,以及为什么在涉及 LINQ 的情况下实际上无法将其与 EF 一起使用。
First, you cannot use your own queue and service if you have more than one instance of the WPF application running. The SqlDependency infrastructure works per appdomain and if you share a queue then the instances (appdomains) will steal notifications from one another resulting in runtime mayhem. Unless you know exactly what you're doing, just use the default implementation (the just-in-time deployed service/queue/procedure).
The error message is pretty clear: 'When using SqlDependency without providing an options value yada yada yada'. So you do not provide an
options
value, therefore the default is used:At the same time you're overwriting the default service in the
.Start()
call. As I said, better get the default case working first untill you're sure you understand what's going on, before venturing into non-default customized behavior, specially with something so difficult to grok as The Mysterious Notification.Have a look at LinqToCache for how to use Query Notifications with LINQ. The article SqlDependency based caching of LINQ Queries explains how this integration works with LinqToSQL and LinqToEF, and also why you cannot actually use it with EF if LINQ is involved.