[英]Weird caching issue with ASP.net/Linq
I'm writing an application involving storing a profile. 我正在编写一个涉及存储配置文件的应用程序。 I'm using Linq to access the database, but having a weird issue when saving a profile. 我正在使用Linq访问数据库,但是在保存配置文件时出现了一个奇怪的问题。 When I save it, it writes to the DB correctly - but when I leave the page and come back, the old values still remain in profile form. 当我保存它时,它会正确地写入数据库-但是当我离开页面并返回时,旧值仍然保留在配置文件形式中。
My profile page: 我的个人资料页面:
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;
.....
}
And my Student class: 而我的学生班:
public static Profile GetProfile(int uID)
{
var profile = (from p in db.Profiles
where p.uID == uID
select p).FirstOrDefault();
return profile;
}
I'm not doing any fancy caching anywhere, so not sure where the old values are stored... 我不在任何地方进行任何高级缓存,因此不确定旧值存储在哪里...
** EDIT ** **编辑**
So, it seems that it's down to a global LinqDataContext. 因此,这似乎取决于全局的LinqDataContext。 In my Student class, I had: 在学生班上,我有:
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;
}
If I give the GetProfile method it's own DataContext, problem solved. 如果我给GetProfile方法是它自己的DataContext,那么问题就解决了。
Still being very new to Linq, what's the best way to have a class with numerous methods that use the same access to a database? 对于Linq还是一个新手,拥有具有使用相同数据库访问权的众多方法的类的最佳方法是什么? Having a global context like this? 有这样的全球背景吗? Or each method using it's own data context? 还是每个使用自己的数据上下文的方法?
Assuming you're storing the userID
in Session["userID"]
somewhere and not clearing it out when you save, this could be where the "caching" occurs. 假设您将userID
存储在Session["userID"]
某个位置,并且在保存时未将其清除,则可能是发生“缓存”的地方。 Session
objects will live (roughly) for the life of the browser session (or the life of the server process if in-memory session is enabled). Session
对象将在浏览器会话的生命周期内(或在启用内存会话的情况下,在服务器进程的生命周期内)生存(大约)。
我想即使linq将查询发送到数据库,它也会使用存储在cache中的旧值。因此,如果要使用新值,则必须清除缓存。
Going on your added comments my guess is that the page you are getting is cached by the browser / http server. 继续添加您的评论,我猜是浏览器/ http服务器缓存了您正在获取的页面。 If the url is the same as a previously requested page some default setting will tell the browser / server to use the cache html. 如果url与先前请求的页面相同,则某些默认设置将告诉浏览器/服务器使用缓存html。 To avoid this and get new html for each request you could try adding a meta tag within the head tags of your html. 为了避免这种情况并为每个请求获取新的html,您可以尝试在html的head标签内添加一个meta标签。
<meta http-equiv="Pragma" CONTENT="no-cache">
It ended up being the DataContext I was using. 最终成为我正在使用的DataContext。 I'm not exactly sure why this fixed the issue, but I changed my class from: 我不确定为什么可以解决此问题,但是我将班级从:
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;
}
}
to: 至:
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.