1.开启数据库缓存依赖功能(开启对数据库中表Article和ArticleType的缓存)
(注:)如果要配置SqlCacheDependency,则需要以命令行的方式执行。
aspnet_regsql.exe工具位于Windows\Microsoft.NET\Framework\[版本]文件夹中。
aspnet_regsql -C "data source=WIN-2FNH16TMA0FHZM;initial catalog=Sngovweb_sta3;user id=Sngovweb_sta;password=sngo1234...." -ed -et -t "dbo.Article"
aspnet_regsql -C "data source=WIN-2FNH16TMA0FHZM;initial catalog=Sngovweb_sta3;user id=Sngovweb_sta;password=sngo1234...." -ed -et -t "dbo.ArticleType"
2.配置Web.config文件
<connectionStrings>
//实体框架生成的数据库连接字符串
<add name="SimpleNewsContext" connectionString="metadata=res://*/SimpleNews.csdl|res://*/SimpleNews.ssdl|res://*/SimpleNews.msl;provider=System.Data.SqlClient;provider connection string="data source=WIN-2FNH16TMA0FHZM;initial catalog=Sngovweb_sta3;persist security info=True;user id=Sngovweb_sta;password=sngo1234....;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
//新增数据库连接字符串
<add name="test" connectionString="data source=WIN-2FNH16TMA0FHZM;initial catalog=Sngovweb_sta3;persist security info=True;user id=Sngovweb_sta;password=sngo1234....;multipleactiveresultsets=True;"/>
</connectionStrings>
<system.web>
<caching>
<sqlCacheDependency enabled="true" pollTime="10000">
<databases>
<add name="SimpleNews" connectionStringName="test" />//这个连接字符串名称不是实体框架生成的那个,而是自己手动添加的
</databases>
</sqlCacheDependency>
</caching>
</system.web>
3.在控制器的方法上打上[OutputCache]标签
[OutputCache(Duration = 86400, SqlDependency = "SimpleNews:dbo.Article;SimpleNews:dbo.ArticleType")]
public ActionResult Index()
{
return View();
}