[英]SharpRepository.EntityFramework caches data even if I tell it not to, how can I prevent it?
I am using SharpRepository.EntityFramework, with default configuration.我正在使用 SharpRepository.EntityFramework,默认配置。 Defined the repository in app.config and ... well it's a big application so I'll just show you the relevant code snippet :
在 app.config 中定义了存储库……嗯,这是一个很大的应用程序,所以我将向您展示相关的代码片段:
IEnumerable<IntegrationQueue> queue_list =
qrepo.FindAll(item => item.IntegrationID == Integration.ID
&& (DateTime.Now > item.NextTry
&& item.Lock == false
&& item.Status != StatusEnum.Success && item.Status != StatusEnum.GaveUp)
|| item.Command != CommendEnum.None);
foreach (IntegrationQueue iq in queue_list)
{
Lock(iq);
Logger.Instance.Trace("Processing record " + iq.ToString());
So this part of the code is called once every 15 seconds.所以这部分代码每 15 秒调用一次。 It gets a list of records from the database that it needs to process.
它从需要处理的数据库中获取记录列表。
Here's the strange thing.这是奇怪的事情。 Say that I now go into SQL Management Studio and update a record and set Command to something else than 0 (None).
假设我现在进入 SQL Management Studio 并更新记录并将 Command 设置为 0(无)以外的其他值。 On the next pass the FindAll() will get a record into queue_list !
在下一次通过 FindAll() 将获得一个记录到 queue_list ! Yay!
好极了!
But then i look into the record, and Command is 0 (None).... What?但是后来我查看了记录,Command 为 0(无)......什么? How??
如何?? The FindAll() triggered on that Command was != 0 (None) !
在该命令上触发的 FindAll() 是 != 0 (None) !
So the FindAll() matching seems to work, but then it gives me a cached version.所以 FindAll() 匹配似乎有效,但它给了我一个缓存版本。 That is bad.
那很不好。
I tried qrepo.CacheEnabled = false, and I tried qrepo.ClearCache() but to no avail.我试过 qrepo.CacheEnabled = false,我试过 qrepo.ClearCache() 但无济于事。
I tried using GetAll() instead of FindAll() (not sure exactly what the difference is) but then it wouldn't even trigger on the record.我尝试使用 GetAll() 而不是 FindAll() (不确定到底有什么区别)但它甚至不会在记录上触发。
Please advice?请指教? Thank you!
谢谢!
I found this old topic, remember to tag it sharp-repository!我找到了这个老话题,记得标记为sharp-repository!
Problem is not related to SharpRepository, if you create a normal application like this:问题与 SharpRepository 无关,如果您创建这样的普通应用程序:
var db = new DemoDbContext();
// check if John already exists
Person person = db.Persons.Single(item => item.Name == "John Smith");
if (person == null)
{
// otherwise create and add him
Person p = new Person()
{
Name = "John Smith",
Age = 25
};
db.Persons.Add(p);
}
// forever
while (true)
{
Person p = db.Persons.Single(item => item.Name == "John Smith");
Console.WriteLine("Found him by name! Age: " + p.Age);
Person p2 = db.Persons.Single(item => item.ID == 3);
Console.WriteLine("Got person ID 3! Age: " + p2.Age);
Console.WriteLine("Waiting three seconds...\n");
Thread.Sleep(1000 * 3);
}
you will have same behavior.你会有同样的行为。
Solution is reinstantiate dbcontext (or repository), in every loop or use AsNoTracking.解决方案是在每个循环中重新实例化 dbcontext(或存储库)或使用 AsNoTracking。
in SharpRepository you can call repo.AsQueryable().AsNoTracking()
在 SharpRepository 你可以调用
repo.AsQueryable().AsNoTracking()
or in new 2.1 prerelease you can set in FetchStrategy guide here或在新的 2.1 预发布版中,您可以在此处设置 FetchStrategy 指南
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.