יום שלישי 19 יוני 2012 17:27
I've implemented SQL Cache dependency in ASP.NET application. It's working the way it should, but I'm stuck with a strange issue. Here's the scenario.
- I have two tables in the scenario - tblEmployees, tblChats
- EmployeeID of tblEmployees is foreign key in tblChats.
- I enabled SQL dependency on tblChats and it was successful.
- I have a method ListChats which fetches chat messages from cache; if it's empty, then from tblChats.
- I opened my application, I added a chat record in tblChats with EmployeeID = 1. The cache got cleared.
- I executed the method ListChats(), it gets all the chat messages and puts them in the cache.
- I ran an update statement on tblChats and the cache gets cleared again, which is obvious.
- But if I run an update staetment on tblEmployee with EmployeeID = 1 it clears the cache as well. Strangely, it doesn't clear the cache if I run update statement with some other EmployeeID.
Am I missing any setting? I checked tblEmployee there are no triggers on it. Also in table AspNet_SqlCacheTablesForChangeNotification, there's only one record with tablename = tblChats.
I'd appreciate any help with this issue.
יום שלישי 26 יוני 2012 07:47מנחה דיון
Thank you for your question.
I am trying to involve someone more familiar with this topic for a further look at this issue. Sometime delay might be expected from the job transferring. Your patience is greatly appreciated.
Thank you for your understanding and support.
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
יום שישי 29 יוני 2012 19:33
I went ahead and tried this with a c# console application that I had and I dont see the cache being cleared. However I am not sure I have recreated the tables and maybe the code to match your situtation. I used the code below. Is it possible for you to post more details about your database schema and asp.net code?
Bill -- Microsoft CTS
create table tblEmployees (EmployeeID int not null, Name varchar(256), Dept varchar(256))
create table tblChats (ChatID int not null, EmployeeID int not null, ChatMsg varchar(4000))
alter table tblEmployees add constraint PK_tblEmployee_EmployeeID primary key clustered (EmployeeID)
alter table tblChats add constraint PK_tblChats_ChatId primary key clustered (ChatID)
alter table tblChats add constraint FK_EmployeeID foreign key (EmployeeID) references tblEmployees(EmployeeID)
insert into tblEmployees (EmployeeID, Name, Dept) values (1, 'Ted', 'Accounting')
insert into tblEmployees (EmployeeID, Name, Dept) values (2, 'Jane', 'Recieving')
insert into tblEmployees (EmployeeID, Name, Dept) values (3, 'John', 'Human Resources')
insert into tblChats (ChatID, EmployeeID, ChatMsg) values (1, 1, 'Important chat from Ted 1')
insert into tblChats (ChatID, EmployeeID, ChatMsg) values (2, 1, 'Important chat from Ted 1')
insert into tblChats (ChatID, EmployeeID, ChatMsg) values (3, 2, 'Important chat from Jane 1')
select * from tblEmployees e inner join tblChats c on e.EmployeeID = c.EmployeeID
select * from sys.foreign_keys
select * from sys.objects where object_id in (select referenced_object_id from sys.foreign_keys)
private static string mStarterConnectionString = @"Data Source=(local);Database=SqlDependencyTest;Persist Security Info=false; Integrated Security=false;User Id=startUser;Password=startUser";
private static string mSubscriberConnectionString = @"Data Source=(local);Database=SqlDependencyTest;Persist Security Info=false; Integrated Security=false;User Id=subscribeUser;Password=subscribeUser";
static void Main(string args)
// Starting the listener infrastructure...
// Registering for changes...
Console.WriteLine("At this point, you should start the Sql Server ");
Console.WriteLine("Management Studio and make ");
Console.WriteLine("some changes to the tblEmployee table that you'll find");
Console.WriteLine(" in the SqlDependencyTest ");
Console.WriteLine("database. Every time a change happens in this ");
Console.WriteLine("table, this program should be ");
Console.WriteLine("Press enter to quit this program.");
public static void RegisterForChanges()
// Connecting to the database using our subscriber connection string
// and waiting for changes...
= new SqlConnection(mSubscriberConnectionString);
SqlCommand oCommand = new SqlCommand(
"SELECT ChatID, EmployeeID, ChatMsg from dbo.tblChats",
SqlDependency oDependency = new SqlDependency(oCommand);
oDependency.OnChange += new OnChangeEventHandler(OnNotificationChange);
SqlDataReader objReader = oCommand.ExecuteReader();
// Doing something here...
public static void OnNotificationChange(object caller,
Console.WriteLine(e.Info.ToString() + ": " + e.Type.ToString());
- סומן כתשובה על-ידי Iric WenModerator יום שני 02 יולי 2012 03:00