Asp.net mvc 3 数据库条目更改时不更新对象数据

Asp.net mvc 3 数据库条目更改时不更新对象数据,asp.net-mvc-3,razor,Asp.net Mvc 3,Razor,我的网站有一个横幅,在紧急/特殊情况下向访问者显示消息。横幅显示基于对数据库表的调用,该数据库表根据DateTime.Now对象检查开始/完成时间 static private PublicWebEntities db=new PublicWebEntities(); 公共数字 @foreach(Model.CurrentAlert()中的变量项){ @Html.Raw(item.Message) } } 如果更改记录中的开始或结束时间,则横幅将关闭。但当更新记录的文本消息时,显示的是旧消息

我的网站有一个横幅,在紧急/特殊情况下向访问者显示消息。横幅显示基于对数据库表的调用,该数据库表根据DateTime.Now对象检查开始/完成时间

static private PublicWebEntities db=new PublicWebEntities();
公共数字
@foreach(Model.CurrentAlert()中的变量项){

@Html.Raw(item.Message)

} }
如果更改记录中的开始或结束时间,则横幅将关闭。但当更新记录的文本消息时,显示的是旧消息的缓存副本


我正在为此站点使用EF 4.1和MVC3。

由于服务器正在缓存页面,您可以使用
OutputCache
属性来控制保存页面的时间

使用以下命令将“关闭”缓存:

[OutputCache(Location = OutputCacheLocation.None, NoStore = false, Duration = 0)]
public ActionResult Index(){
    //Your code
}
这将禁用您正在调用的用于填充缓存结果的操作

对每会话请求使用基本控制器(在注释中提出) 让控制器控制上下文生命周期的示例

/// <summary>
/// Provides a base implementation for controllers using a single, simple common context
/// </summary>
public class BaseController : Controller
{
    public MyDbContext Db { get; set; }

    public BaseController()
    {
        Db = new MyDbContext();
    }

    /// <summary>
    /// Manages saving the changes back to the database.  This should be performed per action and you shouldn't have to manage it
    /// </summary>
    protected override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (Db.ChangeTracker.HasChanges()) Db.SaveChanges();

        base.OnActionExecuted(filterContext);
    }

    /// <summary>
    /// Release the resources the context uses and close connections if needed
    /// </summary>
    protected override void Dispose(bool disposing)
    {
        if(Db != null) Db.Dispose();

        base.Dispose(disposing);
    }
}
//
///使用单个简单的公共上下文为控制器提供基本实现
/// 
公共类BaseController:控制器
{
公共MyDbContext数据库{get;set;}
公共BaseController()
{
Db=新的MyDbContext();
}
/// 
///管理将更改保存回数据库。此操作应按每个操作执行,您不必管理它
/// 
受保护的覆盖无效OnActionExecuted(ActionExecutedContext筛选器上下文)
{
if(Db.ChangeTracker.HasChanges())Db.SaveChanges();
base.OnActionExecuted(filterContext);
}
/// 
///释放上下文使用的资源,并在需要时关闭连接
/// 
受保护的覆盖无效处置(布尔处置)
{
如果(Db!=null)Db.Dispose();
基地。处置(处置);
}
}

可能是因为我使用的是.NET 4,但OutputCacheLocation.NET出现错误。相反,我使用的是
[OutputCache(Duration=10,VaryByParam=“none”)]public ViewResult Index(){var msg=new AlertMsg();return View(msg);}
它只是不起作用。您的代码返回为“在当前上下文中不存在”。@techmaniac如果您有时间,我会有兴趣查看错误消息的堆栈跟踪。它甚至不会生成此错误。这是VS2010中的一个输出错误。我还感兴趣的是,它对开始时间和结束时间的DB中的更改做出响应,但对文本中的更改没有响应。就像它只是在EF实体中缓存string对象,而不是datetime对象。
/// <summary>
/// Provides a base implementation for controllers using a single, simple common context
/// </summary>
public class BaseController : Controller
{
    public MyDbContext Db { get; set; }

    public BaseController()
    {
        Db = new MyDbContext();
    }

    /// <summary>
    /// Manages saving the changes back to the database.  This should be performed per action and you shouldn't have to manage it
    /// </summary>
    protected override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (Db.ChangeTracker.HasChanges()) Db.SaveChanges();

        base.OnActionExecuted(filterContext);
    }

    /// <summary>
    /// Release the resources the context uses and close connections if needed
    /// </summary>
    protected override void Dispose(bool disposing)
    {
        if(Db != null) Db.Dispose();

        base.Dispose(disposing);
    }
}