none
Clear a local collection without disposing the context RRS feed

  • Question

  • I'm using EF6 and VB.net 2017. I have this code :

    context.Entry(Myobj1).Collection("Child_Itm").Load()
    bindingsource1.datasource=context.Child_itm.Local.Tobindinglist

    The problem is that , after that at some point i want to load the child collection for another object like this :

    context.Entry(Myobj2).Collection("Child_Itm").Load()
    bindingsource1.datasource=context.Child_itm.Local.Tobindinglist

    But after that , on my bindingsource the are all the items loaded from the first and the second call of load. How can i do to clear the local collection before loading new data ? Before calling the second load , I've tried with :

    context.child_itm.local.clear 

    , but the objects still exist on local. I want to remove all items , and not keeping even on Detached status But i want to do that without disposing the context.

    Is there any way ?


    Wednesday, December 12, 2018 4:10 PM

All replies

  • You should destroy the context or make a new collection within the context.

    And here is something  else to consider, if you don't destroy the context and you iterate in a loop over the collection that is still connected to the context, then EF goes back and reads each oject again from the database. You create 100,000 objects in the query. You then loop over the collection and that is 100,000 reads again of the same data for each object in the collection. The collection should be a disconnected collection.

    Wednesday, December 12, 2018 10:07 PM
  • Hi dcode25,

    The DbContext in Entity Framework 6 automatically caches data that it retrieves from your database. but we have several ways to manage force Entity Framework to reload the updated data from the database, depending on which version of Entity Framework you are using and what type of application you are writing.

    1. Disable Tracking using AsNoTracking()
    2. Throw away the DbContext and create a new one
    3. Use an ObjectQuery instead of a DBQuery and set MergeOptions
    4. Refresh the Entities
    5. Detatch the Entities
    6. Call GetDatabaseValues to get the updated values for a single Entity
    7. Use the stale data

    For more information, please refer to:

    http://codethug.com/2016/02/19/Entity-Framework-Cache-Busting/

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. 
    Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. 
    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet. 

    Best regards,

    Zhanlgong


    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, December 13, 2018 5:08 AM
    Moderator