none
How EF cache data RRS feed

  • Question

  • i heard EF cache data to save db round trip. i work with EF6. so does it cache or not?

    what option i need to turn on to cache data by EF which will save db round trip?

    how to proof that EF fetching data from cache instead of db ?

    so please show me how to cache data after when EF fetch from db and how to load data from cache for subsequent call.

    discuss with code example. thanks

    Monday, May 21, 2018 6:46 PM

Answers

  • Hi Sudip_inn,


    The Entity Framework has the following forms of caching built-in:
       1. Object caching – the ObjectStateManager built into an ObjectContext instance keeps track in memory of the objects that have been retrieved using that instance. This is also known as first-level cache.
       2. Query Plan Caching - reusing the generated store command when a query is executed more than once.
       3. Metadata caching - sharing the metadata for a model across different connections to the same model.

    Besides the caches that EF provides out of the box, a special kind of ADO.NET data provider known as a wrapping provider can also be used to extend the Entity Framework with a cache for the results retrieved from the database, also known as second-level caching.

    The Find method in DbSet (APIs included for the first time in EF 4.1) will perform a search in memory before even issuing the query against the database. It’s important to note that two different ObjectContext instances will have two different ObjectStateManager instances, meaning that they have separate object caches.

    For more information, please refer to the following link, please check phrase(3 Caching in the Entity Framework)

    https://msdn.microsoft.com/en-us/library/hh949853%28v=vs.113%29.aspx?f=255&MSPPError=-2147217396

    Here is a sample for your reference.

    using (var db = new Model1())
    {
                    var queryfind = db.Courses.Find(1);
                    Console.WriteLine("CourseID = {0}, Education = {1}", queryfind.courseID, queryfind.education);
    
                    //execute sql statement in database, update Courses set education = 'Test1' where courseID = 1
    
                    var queryfind2 = db.Courses.Find(1);
                    // the result is same with the first retrieve, which from caching. not from database.
                    Console.WriteLine("CourseID = {0}, Education = {1}", queryfind2.courseID, queryfind2.education);
    
    
                    Console.WriteLine("OK");
                    Console.ReadLine();
    }
    

    We could use entity framework extension to implement Second Level Cache, for more information, please refer to:


    https://github.com/moozzyk/EFCache

    In addition, please refer to the following thread.

    Using .Single, .First, .Where, etc will not cache the results unless you are using second-level caching.

    https://stackoverflow.com/questions/21707970/why-does-entity-framework-6-x-not-cache-results/21708204#21708204


    Best regards,

    Zhanglong


    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.

    • Marked as answer by Sudip_inn Sunday, May 27, 2018 6:55 PM
    Tuesday, May 22, 2018 3:33 AM
    Moderator

All replies

  • Hi Sudip_inn,


    The Entity Framework has the following forms of caching built-in:
       1. Object caching – the ObjectStateManager built into an ObjectContext instance keeps track in memory of the objects that have been retrieved using that instance. This is also known as first-level cache.
       2. Query Plan Caching - reusing the generated store command when a query is executed more than once.
       3. Metadata caching - sharing the metadata for a model across different connections to the same model.

    Besides the caches that EF provides out of the box, a special kind of ADO.NET data provider known as a wrapping provider can also be used to extend the Entity Framework with a cache for the results retrieved from the database, also known as second-level caching.

    The Find method in DbSet (APIs included for the first time in EF 4.1) will perform a search in memory before even issuing the query against the database. It’s important to note that two different ObjectContext instances will have two different ObjectStateManager instances, meaning that they have separate object caches.

    For more information, please refer to the following link, please check phrase(3 Caching in the Entity Framework)

    https://msdn.microsoft.com/en-us/library/hh949853%28v=vs.113%29.aspx?f=255&MSPPError=-2147217396

    Here is a sample for your reference.

    using (var db = new Model1())
    {
                    var queryfind = db.Courses.Find(1);
                    Console.WriteLine("CourseID = {0}, Education = {1}", queryfind.courseID, queryfind.education);
    
                    //execute sql statement in database, update Courses set education = 'Test1' where courseID = 1
    
                    var queryfind2 = db.Courses.Find(1);
                    // the result is same with the first retrieve, which from caching. not from database.
                    Console.WriteLine("CourseID = {0}, Education = {1}", queryfind2.courseID, queryfind2.education);
    
    
                    Console.WriteLine("OK");
                    Console.ReadLine();
    }
    

    We could use entity framework extension to implement Second Level Cache, for more information, please refer to:


    https://github.com/moozzyk/EFCache

    In addition, please refer to the following thread.

    Using .Single, .First, .Where, etc will not cache the results unless you are using second-level caching.

    https://stackoverflow.com/questions/21707970/why-does-entity-framework-6-x-not-cache-results/21708204#21708204


    Best regards,

    Zhanglong


    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.

    • Marked as answer by Sudip_inn Sunday, May 27, 2018 6:55 PM
    Tuesday, May 22, 2018 3:33 AM
    Moderator
  • Sir i like to know how EF cache data. when we fetch data from db then EF cache those data or not. if not then how find search data in memory ?

    thanks

    Sunday, May 27, 2018 6:56 PM