DbLinq-缓存问题 [英] DbLinq - Cache problem

查看:131
本文介绍了DbLinq-缓存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在ASP.NET MVC网站中使用linq to sql for MySql(使用DbLinq).我有一个奇怪的缓存问题.在我的存储库类中考虑以下方法:

I'm using linq to sql for MySql (using DbLinq) in an ASP.NET MVC website. I have a weird caching problem. Consider the following methods in my Repository class:

public IEnumerable<Message> GetInbox(int userId)
{
  using(MyDataContext repo = new MyDataContext(new MySqlConnection("[Connectionstring]")))
  {
    return repo.Messages.Where(m => m.MessageTo == userId);
  }
}

public IEnumerable<Message> GetOutbox(int userId)
{
  using (MyDataContext repo = new MyDataContext(new MySqlConnection("[Connectionstring]")))
  {
    return repo.Messages.Where(m => m.MessageFrom == userId);
  }
}

"MyDataContext"是DbLinq生成的映射到我的数据库的映射,该映射继承自DataContext.我不在这里重用datacontext(上面的代码看起来有些愚蠢,但我想绝对确保它不是某些datacontext/mysqlconnection重用问题).

'MyDataContext' is the by DbLinq generated mapping to my database, which inherits from DataContext. I'm not reusing the datacontext here (the above code looks a bit silly but I wanted to make absolutely sure that it was not some datacontext / mysqlconnection re-using issue).

发生的事情是,无论使用哪种userId,无论我调用的是哪种方法,结果都保持不变.时期.即使我可以看到repo.Messages的结果超过10个,并且MessageFromMessageTo的值都不同,但我只能得到最先查询的结果.因此,如果我呼叫GetInbox(4374),它将给我消息A和消息B.此后再呼叫GetInbox(526)仍然给我消息A和B,即使有 个消息 do 的用户ID为526.我必须重新启动应用程序才能查看任何更改.

What happens is, whichever of the two methods I call, with whatever userId, the results stay the same. Period. Even though I can see that repo.Messages has more than 10 results, with varying MessageFrom and MessageTo values, I only get the first-queried results back. So if I call GetInbox(4374) it gives me message A and message B. Calling GetInbox(526) afterwards still gives me message A and B, even though there are messages C and D who do have a userId of 526. I have to restart the application to see any changes.

这是怎么回事?我敢肯定我做的事如此愚蠢,以至于有人向我指出时我会感到羞愧.如果我没有做一些非常愚蠢的事情,那么我觉得这个问题很奇怪.我读到有关不重用DataContext的信息,但我没有.为什么会出现此缓存问题?以下是我的控制器代码,但我怀疑这很重要:

What's going on here? I'm sure I'm doing something so stupid that I'm going to be ashamed when someone points it out to me. If I'm not doing something very stupid, then I find this issue very strange. I read about not reusing DataContext, but I am not. Why this caching issue? Below is my controller code, but I doubt it matters:

[Authorize]
public ActionResult Inbox(int userId)
{
  Mailbox inbox = new Mailbox(userId, this.messageRepository.GetInbox(userId));
  return PartialView("Inbox", inbox);
}

尽管在SO上也有类似的问题,但我还没有找到这个确切问题的答案.非常感谢!

Though there are similar questions on SO, I haven't found an answer to this exact question. Many thanks!

更新: 将代码更改为:return repo.Messages.ToList().Where(m => m.MessageFrom == userId);对其进行了修复,然后可以正常工作.似乎有些缓存问题.但是,我当然不想这样解决. 更改代码,以便在查询后不能解决问题后不处理数据上下文.

UPDATE: changing the code to: return repo.Messages.ToList().Where(m => m.MessageFrom == userId); fixes it, it works fine then. Seems like some cache problem. However, I of course don't want to fix it that way. Changing the code so that the datacontext is not disposed after the query does not fix the problem.

推荐答案

好吧,看来这是DbLinq的问题.我使用了3周前的源代码,并且QueryCache中有一个明显的错误(尽管它一直都存在).有一个完整的线程涵盖了此此处.

Well, it seemed that it was a problem with DbLinq. I used source code from 3 weeks old and there was an apparant bug in QueryCache (though it has always been in there). There's a complete thread that covers this here.

我更新了dblinq源.现在已禁用Querycache(这确实意味着性能下降),并且至少现在可以正常使用了.我必须看看性能是否可以接受.必须承认我有点困惑,尽管我要尝试的是常见的linq2sql模式.谢谢大家.

I updated the dblinq source. Querycache is now disabled (does imply a performance hit) and well at least now it works. I'll have to see if the performance is acceptable. Must confess that I'm a bit baffled though as what I'm trying to do is a common linq2sql pattern. Thanks all.

这篇关于DbLinq-缓存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆