I\'m using the EF 6 async
querying features, such as
var list = await cx.Clients.Where(c => c.FirstName.Length > 0).ToListAsync();
I'm not too familiar with SqlDependency, but the below will allow your CallContext to have the correct value at the time ToListAsync is called (When multiple calls are running). Proof of concept here, https://dotnetfiddle.net/F8FnFe
async Task<List<Client>> GetData()
{
using (ClientsContext context = new ClientsContext()) // subclass of DbContext
{
SqlDependency.Start(context.Database.Connection.ConnectionString);
SqlDependency dependency = new SqlDependency();
dependency.OnChange += (sender, e) =>
{
Console.Write(e.ToString());
};
Task<List<Client>> task = Task<Task<List<Client>>>.Factory.StartNew(async () =>
{
System.Runtime.Remoting.Messaging.CallContext.SetData("MS.SqlDependencyCookie", dependency.Id);
var list = await context.Clients.Where(c => c.FirstName.Length > 0).ToListAsync();
}).Unwrap();
return await task;
}
}