1

I have a service and a webapplication using the same database, and common library for the database context. I'm disposing my database contexts, however when I manually insert a row on the database it is not shown in the webapp but works in the service.

By logging what Entity Framework does I noticed the following difference:

SERVICE - Completed in 8 ms with result: EFMySqlDataReader
WEBAPP  - Completed in 16 ms with result: CachingReader

So I suspect it has something to do with that the webapp uses the cached result instead of querying the database. Is there a way to force to query the database?

--

For completeness, it produces both the same query:

public static async Task<int> CountMailsInQueueAsync()
{
    using (var ctx = new GdprContext())
    {
        ctx.Database.Log = s => Log.Debug(s);

        // SELECT `GroupBy1`.`A1` AS `C1`
        // FROM
        //   (SELECT COUNT(1) AS `A1`
        //    FROM `tbl_email` AS `Extent1`
        //    WHERE (0 = (`Extent1`.`MailStatus`))
        //      OR (1 = (`Extent1`.`MailStatus`))) AS `GroupBy1`;

        return await ctx.Emails
            .Where(e => e.MailStatus == InfMailStatus.Ready || e.MailStatus == InfMailStatus.Processing)
            .CountAsync();
    }
}

// same connection string: 
// "server=localhost;user=dev;database=dev;port=3306;password=none;charset=utf8;Allow User Variables=True;Convert Zero Datetime=True;Allow Zero Datetime=True"
//
// using MySql.Data.Entity v6.9.9
5
  • Have you tried to query several times in a row (like 10 times) the same provider and see if the CachingReader is really that slower than the DB? Commented Jan 8, 2018 at 15:12
  • 1
    EF does not cache query results. Looks like you are using EF Cache package. Commented Jan 8, 2018 at 15:12
  • @IvanStoev, I think with proxy and tracking, EF (at least 6) tracks entities. That is, if with the same context instance you load twice the same entity, the result of the second loading wil not be used to update the result of the first loading. Of course this behaviour should not impact the result of a Count(). Commented Jan 8, 2018 at 15:24
  • @tschmit007 Tracking has nothing to do with second level cache. Also EF6 has no CachingReader class while the 3rd party package from the link does. Commented Jan 8, 2018 at 15:30
  • 1
    @IvanStoev ah I see, you're correct. I wasn't aware of that package in this project. If you add it as an answer I can mark it as correct. Commented Jan 8, 2018 at 15:35

1 Answer 1

3

EF does not cache query results (the so called second level cache).

The name CachingReader indicates that the WEBAPP project is using EF Cache package. Find how it is setup in that project and read the package documentation how to ignore/force cache rebuild (there must be a Purge method or something like that).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.