CodeOrElse
CodeOrElse

Reputation: 328

SharpRepository.EntityFramework caches data even if I tell it not to, how can I prevent it?

I am using SharpRepository.EntityFramework, with default configuration. Defined the repository in app.config and ... well it's a big application so I'll just show you the relevant code snippet :

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. 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). On the next pass the FindAll() will get a record into queue_list ! Yay!

But then i look into the record, and Command is 0 (None).... What? How?? The FindAll() triggered on that Command was != 0 (None) !

So the FindAll() matching seems to work, but then it gives me a cached version. That is bad.

I tried qrepo.CacheEnabled = false, and I tried qrepo.ClearCache() but to no avail.

I tried using GetAll() instead of FindAll() (not sure exactly what the difference is) but then it wouldn't even trigger on the record.

Please advice? Thank you!

Upvotes: 0

Views: 167

Answers (1)

fiorebat
fiorebat

Reputation: 3451

I found this old topic, remember to tag it sharp-repository!

Problem is not related to SharpRepository, if you create a normal application like this:

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.

in SharpRepository you can call repo.AsQueryable().AsNoTracking()

or in new 2.1 prerelease you can set in FetchStrategy guide here

Upvotes: 1

Related Questions