8

How can we apply Sql Dependency in Asp.Net MVC for cached objects?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Rishabh Ohri
  • 1,280
  • 4
  • 16
  • 28
  • 3
    It would help if you included more detail in your question. It sounds like you're asking about using a SqlDependency when storing objects in the cache. But that is no different than using the cash in straight ASP.NET, without MVC. Output caching, on the other hand, is a different story. If you put a little more effort into your question, you might get a better answer. – Craig Stuntz Feb 03 '10 at 14:50

2 Answers2

3

Also you can use SqlDependency with Entity Framework 5.you use following link: http://www.codeproject.com/Articles/496484/SqlDependency-with-Entity-Framework-5-0

Hossein Moradinia
  • 6,116
  • 14
  • 59
  • 85
3

MSDN says:

To set up a dependency, you need to associate a SqlDependency object to one or more SqlCommand objects. To receive notifications, you need to subscribe to the OnChange event.

If you are using an ORM for your data model, I'm not sure how this applies.

More info here:

http://msdn.microsoft.com/en-us/library/t9x04ed2.aspx

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • 1
    Well, SqlDependency requires a SQL query which can only use features allowed in SQL Server query notifications. Since your ORM very likely does not know about this feature, it means you have to write a standard SQL query. Your ORM will very likely be able to give you the SQL query it will use to execute a certain ORM-style query. So your choices are to manually write the query which covers the data which is likely to change, or use the ORM's query, and hope that it falls within the rules of what is allowed for a SQL Server query notification. – Craig Stuntz Feb 03 '10 at 14:47
  • 3
    One other thing I forgot to add: Query notifications put a certain degree of load on the SQL Server. So you would prefer to use as few of them as possible. Therefore, if you are composing queries via your ORM, you probably don't want to create a query notification for every ORM query you compose. You would probably prefer to use a single, manually written query, which could provide appropriate notifications for a wide variety of ORM queries which touch the same data. – Craig Stuntz Feb 03 '10 at 14:51