简体   繁体   English

SharpRepository.EntityFramework 即使我告诉它不要缓存数据,我该如何防止它?

[英]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.

相关问题 如何为SharpRepository的InMemoryRepository提供种子? - How can I seed SharpRepository's InMemoryRepository? 如何使用SharpRepository进行$ regex调用? - How can I do a $regex call with SharpRepository? 如何创建一个缓存对象的类? - How can I create a class that caches objects? 如何在WPF中使用EntityFramework将数据绑定到DataGridView - How Can I Bind Data To DataGridView With EntityFramework In WPF EntityFramework 如何按日期时间删除行 - EntityFramework How Can I Delete Lines by DateTime 如何将条件订单与 EntityFramework/linq 一起使用 - How can I use conditional Order with EntityFramework/linq 每当我保存datagridview时,它都会保存所有记录,即使它们在SQL表中也是如此。 我该如何预防呢? - whenever i am saving datagridview it saves all the records , even if they are in SQL table. how can I prevent this? 如何使用 EntityFramework 获取表名 - How I can get table name using EntityFramework 如何在 EntityFramework 中配置这种关系(1 -&gt; 许多,加 1 -&gt; 0 或 1,无连接表) - How can I configure this relationship in EntityFramework (1 -> many, plus 1 -> 0 or 1, no join table) 如何重置EntityFramework .NET Core WebServer的ConnectionString - How can I reset the ConnectionString of EntityFramework .NET Core WebServer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM