none
Entity Framework Core caching RRS feed

  • Question

  • Hi all,

    I have an API application made in .NET Core using Entity Framework Core. There are currently running 2 instances of the application load balanced connecting to the same database.

    When data is changed in one instance, it is written to the database so that's all working. Now when I press F5 a couple of times, I get connected to the other instance which still has the old data in there. I think it's cached in memory. How can I configure EF to not use caching at all? I can find some information about EF6 but nothing on EF Core.

    Thanks!

    • Moved by Sabah ShariqMVP Wednesday, February 22, 2017 11:29 AM Moved From Visual C#
    Wednesday, February 22, 2017 11:18 AM

All replies

  • Hi Raven-ix,

    >> I think it's cached in memory. How can I configure EF to not use caching at all?

    I think that you could use No-tracking queries, which are useful when the results are used in a read-only scenario. They are quicker to execute because there is no need to setup change tracking information. 

    You can swap an individual query to be no-tracking:

    using (var context = new BloggingContext())
    {
        var blogs = context.Blogs
            .AsNoTracking()
            .ToList();
    }
    You can also change the default tracking behavior at the context instance level:
    using (var context = new BloggingContext())
    {
        context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
    
        var blogs = context.Blogs.ToList();
    }
    

    For more information, please refer to:

    https://docs.microsoft.com/en-us/ef/core/querying/tracking

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 23, 2017 8:40 AM
    Moderator