none
Clearing the database context cache - Ok to do? RRS feed

  • Question

  • I want to be able to clear the database context cache in order to clear the entities the context is currently tracking. The context object has a ClearCache method, but it's internal. I wrote the following code to call this method:

            private void ClearContextCache()
            {
                const BindingFlags FLAGS = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
                var method = this.GetType().GetMethod("ClearCache", FLAGS);
                method.Invoke(this, null);
            }

    The method works fine. But I'm concerned about whether clearing the context cache is a safe thing to do. I assume there is a reason why the L2S architects at Microsoft didn't want developers calling this method. Anyone have experience with this?

    Thanks - Randy

    Tuesday, May 18, 2010 1:01 PM

Answers

  • Hi Randy,

     

    Glad to see you again!

     

    First, DataContext is intended to be a short life object, which is designed for a unit of work.   A DataContext is lightweight and is not expensive to create.   A typical LINQ to SQL application creates DataContext instances at method or as a member of short-lived classes that represent a logical set of related database operations.   Based on your scenario, how about recreating a new DataContext?   Personally, I don’t think calling ClearCache has any problems, but since all the cache data is removed, the DataContext will be similar with a new one.   Besides, Reflection is not good in performance.  I would recommend you create a new DataContext instead of calling ClearCache.   

     

    Have a nice day!

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Randy Minder Wednesday, May 19, 2010 11:33 AM
    Wednesday, May 19, 2010 1:52 AM
    Moderator

All replies

  • Hi Randy,

     

    Glad to see you again!

     

    First, DataContext is intended to be a short life object, which is designed for a unit of work.   A DataContext is lightweight and is not expensive to create.   A typical LINQ to SQL application creates DataContext instances at method or as a member of short-lived classes that represent a logical set of related database operations.   Based on your scenario, how about recreating a new DataContext?   Personally, I don’t think calling ClearCache has any problems, but since all the cache data is removed, the DataContext will be similar with a new one.   Besides, Reflection is not good in performance.  I would recommend you create a new DataContext instead of calling ClearCache.   

     

    Have a nice day!

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Randy Minder Wednesday, May 19, 2010 11:33 AM
    Wednesday, May 19, 2010 1:52 AM
    Moderator
  • Thanks Lingzhi for your suggestion. 

     

    Randy

    Wednesday, May 19, 2010 11:34 AM