-->

NCache as Entity Framework second level cache not

2019-07-30 16:22发布

问题:

Using NCache 4.6 as EF 6.1.3 second level cache, SqlDependency is not working. (It cant detect changes that I make directly in SQL server. Also does not update cache or invalidate it when using EF to add or delete entities to database. Albeit it detect updating an entity)

I've installed NCache in my computer with IP: 192.168.56.1 and SQL server 2014 in vbox with IP: 192.168.56.101

using instruction from their site and customizing them to meet my IP & user, I executed following SQL:

ALTER DATABASE NORTHWND SET ENABLE_BROKER with rollback immediate;
GO
--Select is_broker_enabled from sys.databases where name ='NORTHWND'

GRANT SUBSCRIBE QUERY NOTIFICATIONS TO user1
GRANT CREATE QUEUE TO user1
GRANT CREATE SERVICE TO user1
GRANT CREATE PROCEDURE TO user1


CREATE QUEUE "NCacheSQLQueue-192.168.56.1";
CREATE SERVICE "NCacheSQLService-192.168.56.1" ON QUEUE."NCacheSQLQueue-192.168.56.1"([http://schemas.microsoft.com/SQL/Notifications/PostQueryNotification]);

GRANT SUBSCRIBE QUERY NOTIFICATIONS TO user1;
GRANT RECEIVE ON "NCacheSQLQueue-192.168.56.1" TO user1;
GRANT RECEIVE ON QueryNotificationErrorsQueue TO user1;
GRANT SEND ON SERVICE :: "NCacheSQLService-192.168.56.1" to user1;

Also, select * from sys.dm_qn_subscriptions shows that no one is subscribed.

回答1:

It turns out that I needed to run ALTER AUTHORIZATION ON DATABASE::[NORTHWND] TO user1;.

Here is what I did:

1- restored a fresh Northwind database.

2- ran following SQL:

ALTER DATABASE NORTHWND SET ENABLE_BROKER with rollback immediate;
GO

GRANT SUBSCRIBE QUERY NOTIFICATIONS TO sa
GRANT CREATE QUEUE TO sa
GRANT CREATE SERVICE TO sa
GRANT CREATE PROCEDURE TO sa

ALTER AUTHORIZATION ON DATABASE::[NORTHWND] TO sa

Note that in this query I intentionally omitted CREATE QUEUE "NCacheSQLQueue-192.168.56.1"; and all queries after that.

Also, I found out that Entity Framework Profiler is incompatible with either NCache or SQL Dependency, so I disabled it.