gpt4 book ai didi

c# - Entity Framework 似乎在缓存数据

转载 作者:行者123 更新时间:2023-12-05 09:21:32 24 4
gpt4 key购买 nike

我的问题是,当我编写一个基本的 EF 查询以从数据库中获取数据时,它可以很好地检索数据,但是当我更改 SQL Server 数据库中的数据并重新加载查询时,我会得到相同的数据集,而不是新数据。并且修改后的数据需要一段时间。

var mm = o.GetContent(page, title);

例如上面的查询会带回

mm.Body = "Test";

然后,如果我将 SQL Server 数据库中的 Body 更改为 Test1 并重新加载查询,它不会返回 Test1

public String GetContent(String page, String title)
{
var o = new DataContext();

var mm = o.GetContent(page, title);

return HttpUtility.HtmlDecode(mm.Body);
}

public class DataContext
{
private static ApplicationDbContext Da = new ApplicationDbContext();

public Content GetContent(String page, String title)
{
return Da.Content.SingleOrDefault(c => c.Page == page && c.Title == title);
}
}

我访问过许多 SO 帖子:

Prevent Caching in ASP.NET MVC for specific actions using an attribute

ASP.NET MVC how to disable automatic caching option?

最佳答案

您的 DbContext 实例是静态的:

private static ApplicationDbContext Da = new ApplicationDbContext();

因此,对于每个 AppDomain,它一直存在,直到您回收应用程序池。 DbContext 保存它之前看到的项目的缓存,直到你 call the Refresh() method .

但是不要那样做。不要让它静止。使用 Entity Framework 作为一个工作单元,尽可能缩小范围。每个请求创建一个实例。

关于c# - Entity Framework 似乎在缓存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31404229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com