SqlDependency with EntityFramework 6 (async)

asynchronous c# entity-framework sqldependency


I'm employing the EF 6.async querying capabilities like

var list = await cx.Clients.Where(c => c.FirstName.Length > 0).ToListAsync();

In order to be notified when the data in the database changes, I also want to start SQL dependencies on certain queries. With the help of theSystem.Runtime.Remoting.Messaging.CallContext the following

    async Task GetData()
        using (ClientsContext context = new ClientsContext()) // subclass of DbContext

            SqlDependency dependency = new SqlDependency();
            dependency.OnChange += (sender, e) =>

            System.Runtime.Remoting.Messaging.CallContext.SetData("MS.SqlDependencyCookie", dependency.Id);
            var list = await context.Clients.Where(c => c.FirstName.Length > 0).ToListAsync();

And it runs smoothly. But if I want to make an argument, I am having trouble.SqlDependency on many queries. If I possess twoasync approaches comparable toGetData() If I run both of the aforementioned programs at once, only the first will receive change notifications. I presume that this is because each procedure successively setting the cookie in the CallContext. If I hold off until the firstasync after the first procedure is finished, they both receive change notices as predicted. Is there a way to fix this?

6/27/2013 5:07:55 AM

Popular Answer

6/3/2014 10:41:59 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow