0

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??

Dustin Laine
38.6k10 gold badges90 silver badges125 bronze badges
asked Nov 1, 2010 at 20:01

1 Answer 1

4

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:

The notification request options to be used by this dependency. null to use the default service.

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.

answered Nov 1, 2010 at 20:35
3
  • thanks for your answer. I tried using LinqToCache and still get the same exception thrown?? Commented Nov 1, 2010 at 21:25
  • Try initializing the SqlDependency w/o a custom service/queue first and see if you can get a simple SqlCommand to work. Commented Nov 1, 2010 at 21:34
  • tried that and got it working, as you stated Query Notifications don't work with LinqToEF / Entity Framework. Thanks. Commented Nov 1, 2010 at 21:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.