[英]Weird caching issue with ASP.net/Linq
我正在编写一个涉及存储配置文件的应用程序。 我正在使用Linq访问数据库,但是在保存配置文件时出现了一个奇怪的问题。 当我保存它时,它会正确地写入数据库-但是当我离开页面并返回时,旧值仍然保留在配置文件形式中。
我的个人资料页面:
if(!Page.IsPostBack) {
Profile p = Student.GetProfile(Int32.Parse(Session["userID"].ToString()));
if (p != null)
{
FirstNameTextBox.Text = p.FirstName;
LastNameTextBox.Text = p.LastName;
Address1TextBox.Text = p.Address1;
.....
}
而我的学生班:
public static Profile GetProfile(int uID)
{
var profile = (from p in db.Profiles
where p.uID == uID
select p).FirstOrDefault();
return profile;
}
我不在任何地方进行任何高级缓存,因此不确定旧值存储在哪里...
**编辑**
因此,这似乎取决于全局的LinqDataContext。 在学生班上,我有:
public class Student
{
private static LinqClassesDataContext db = new LinqClassesDataContext() { CommandTimeout = 36000 };
public static Profile GetProfile(int uID)
{
var profile = (from p in db.Profiles
where p.uID == uID
select p).FirstOrDefault();
return profile;
}
如果我给GetProfile方法是它自己的DataContext,那么问题就解决了。
对于Linq还是一个新手,拥有具有使用相同数据库访问权的众多方法的类的最佳方法是什么? 有这样的全球背景吗? 还是每个使用自己的数据上下文的方法?
假设您将userID
存储在Session["userID"]
某个位置,并且在保存时未将其清除,则可能是发生“缓存”的地方。 Session
对象将在浏览器会话的生命周期内(或在启用内存会话的情况下,在服务器进程的生命周期内)生存(大约)。
我想即使linq将查询发送到数据库,它也会使用存储在cache中的旧值。因此,如果要使用新值,则必须清除缓存。
继续添加您的评论,我猜是浏览器/ http服务器缓存了您正在获取的页面。 如果url与先前请求的页面相同,则某些默认设置将告诉浏览器/服务器使用缓存html。 为了避免这种情况并为每个请求获取新的html,您可以尝试在html的head标签内添加一个meta标签。
<meta http-equiv="Pragma" CONTENT="no-cache">
最终成为我正在使用的DataContext。 我不确定为什么可以解决此问题,但是我将班级从:
public class Student
{
private static LinqClassesDataContext db = new LinqClassesDataContext() { CommandTimeout = 36000 };
public static Profile GetProfile(int uID)
{
var profile = (from p in db.Profiles
where p.uID == uID
select p).FirstOrDefault();
return profile;
}
}
至:
public class Student
{
public static Profile GetProfile(int uID)
{
LinqClassesDataContext db = new LinqClassesDataContext();
var profile = (from p in db.Profiles
where p.uID == uID
select p).FirstOrDefault();
return profile;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.