Disable caching for LINQ queries in CRM 2011


  • I have a WinForm application which is making LINQ queries to a CRM 2011 application. When I run a query I am finding that caching is enabled by default so the application is not picking up changes made in CRM unless I restart the WinForm application.

    I found one post which suggests caching can be disabled by updating the configuration file to include the section below but this does not seem to have any effect - any thoughts on what I am doing wrong or any alternative approaches.

        <section name="microsoft.xrm.client" type="Microsoft.Xrm.Client.Configuration.CrmSection, Microsoft.Xrm.Client"/>

          <add name="Xrm" type="Xrm.XrmServiceContext, Xrm" serviceName="Xrm"/>
          <add name="Xrm" type="Microsoft.Xrm.Client.Services.OrganizationService, Microsoft.Xrm.Client"/>

    Nigel T

    Thursday, March 22, 2012 11:45 AM

All replies

  • hi Nigel

     Refer this article



     if the response answered your question, please take a minute and mark the response as an answer. 

    Thursday, March 22, 2012 11:53 AM
  • Hi

    Thanks for the quick reply. That looks to do exactly what I want but the problem I have is that the Microsoft.Xrm.Client.Caching.CacheManager namespace does not seem to exist in the CRM 5 SDK - it looks to be a CRM4 SDK entry. In my project I have a reference to Microsoft.xrm.client.dll (version 5.0.9688.1154) but this only seem to have the Microsoft.xrm.client.caching.objectcachemanager namespace - am I missing a reference?



    Nigel T

    Thursday, March 22, 2012 1:15 PM
  • I'm doing this in CRM 5,

    System.Runtime.Caching.ObjectCache cache = Microsoft.Xrm.Client.Caching.ObjectCacheManager.GetInstance();

    The dll's you need to reference are located in the sdk/bin folder

    • Proposed as answer by Iain_Wright Thursday, March 06, 2014 7:42 AM
    Thursday, March 22, 2012 2:35 PM
  • Hi

    Did this help?

    Please make sure to mark as answer to the response that helped you get through. This will help others with similar problem identify the answer and also close this thread as resolved.



    Thursday, March 22, 2012 3:37 PM
  • You can  definitely reference the ObjectCacheManager 5.0 (2011) namespace  . If you look up ObjectCacheManager in the sdk - it covers the implementation of the Clear and RemoveAll methods (in addition to how to add things and change the way the policies work). KBalz code below covers clear which should get you waht you want but take a look at the SDK just for reference on how to work with the cache in general - there's quite a bit of flexibility there that it sounds like you'll want to take advantage of.

    Cordially, W.G.Ryan -

    Thursday, March 22, 2012 3:44 PM
  • I have tried that but it does not seem to have any effect. Stepping thru the code I can see that the Objectcache has a name or 'XRM' so it looks to be the right ccahe entity but the Clear command does not have any effect - maybe a permissions thing?


    Nigel T

    Thursday, March 22, 2012 3:48 PM
  • I suggest using the fully qualified namespace name if there is question if you're calling the correct class or not.

    We may need to see some of your code. I did not use any special permissions to use the code I pasted earlier. You are calling the Clear command prior to your LINQ query correct?

    Thursday, March 22, 2012 4:59 PM
  • My test code is very simple. All I do is read the entire entity and display it in a propertygrid:

    Dim ObjectCache As System.Runtime.Caching.ObjectCache

    ObjectCache = Microsoft.Xrm.Client.Caching.ObjectCacheManager.GetInstance

    Dim Customer = (From Contact In _EServicesContext.ContactSet Where Contact.LastName.Equals("CRMTester")).FirstOrDefault

    If Customer IsNot Nothing Then
       PropertyGrid.SelectedObject = Customer
    End If

    Whats I do is

    1. Run the code above and view the result
    2. Go to CRM and update and save a property on the entity contact where the last name is 'CRMTester'
    3. Run the code above again and view the results - that changes made at step 2 are not shown and tehr esults are the same as at step 1.

    I know the updates to CRM are working as I have a second (non-LINQ) based query and this shows the updates correctly every time.

    Many thanks for all your help with this.

    Nigel T

    Thursday, March 22, 2012 5:25 PM
  • This page shows some of the ways to configure caching as well as other behaviors for the service context:

    Note that the uncached example there is swapping out the default CachedOrganizationService (which implements caching) with OrganizationService (which does not implement caching).

    Shan McArthur Check out the commercial edition of xRM portals @

    Sunday, March 25, 2012 11:11 PM
  • Below are the configuration settings for removing the caches fom xrm object.

        <section name="microsoft.xrm.client" type="Microsoft.Xrm.Client.Configuration.CrmSection, Microsoft.Xrm.Client" />


        <add name="Crm" connectionString="Authentication Type=Integrated; Server=http://imslnkcrm03/nextstep; " />

        <contexts default="Default">   
          <add name="CacheDisabled" type="Namespace.DataContextClassname, ActionPlanReportGenerator" connectionStringName="Crm" serviceName="CacheDisabled" />
        <services default="Default">
          <add name="CacheDisabled" type="Microsoft.Xrm.Client.Services.OrganizationService" />

    >Net Code

     XrmDataContext crm = new XrmDataContext("CacheDisabled");

    Thanks, chandra

    Tuesday, October 16, 2012 9:34 AM
  • This worked for me based on using early bound classes and LINQ query to Microsoft Dynamics CRM 2013 online.

    Iain Wright

    Thursday, March 06, 2014 7:42 AM