none
Force entity framework to load new data from database without disposing the context RRS feed

  • Question

  • Hello !
    I'm using VB.net 2017 , Entity framework 6.
    On my form I have loaded several entities and its childs.

    Mylist=context.myobjs.Include("mychilds").Tolist.

    Now , another user that use the same program from another computer , make some changes in some of the records and/or some childs. He save the data to database.

    On my form I have a refresh button that execute the same query as above.
    The problem is that when I press this button , on my list I have no updated values from database but the old values. It seems that that query does not read the database but use the data on local cache ( Why ???) .(Note this problem is only with Updated records , the deleted and new added have no problems.)

    I know that disposing the context and creating a new one will resolve this problem , but I don't want to do this because on my forms I have other records from other entities related with each other that I have loaded . And if I recreate the context I need to load again all these but I don't want to do this because only records form 1 entity and its childs can be updated from other users as I described above.

    Searching in internet , I've found 2 general methods doing this :

    METHOD 1

    For Each en As myobj In mylist
        context.Entry(en).Reload()
        For Each chld In en.mychilds
           context.Entry(chld).Reload()
         Next
    Next

    METHOD 2

    For Each en As myobj In mylist
     For i= en.mychilds.count-1 to 0 step -1
      context.Entry(en.mychilds(i)).State = EntityState.Detached
     Next
     context.Entry(en).State = EntityState.Detached
    Next
    Mylist=context.myobjs.Include("mychilds").Tolist

    The problem is that both methods are very slow when mylist is very large.

    Is there any other way ?

    Thank you !

    Wednesday, April 3, 2019 3:12 PM

All replies

  • You can try using raw t-sql, or you can call a stored procedure using EF.

    http://www.entityframeworktutorial.net/EntityFramework4.3/raw-sql-query-in-entity-framework.aspx

    Thursday, April 4, 2019 6:19 AM
  • Hi,

    Have you tried to use DbEntityEntry.ReloadAsync Method instead of "Reload"?

    Regards,

    Kyle


    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, April 4, 2019 9:56 AM
  • Sorry , I found a new way :
    Hide   Copy Code
    Dim contextobj = (CType(context, IObjectContextAdapter)).ObjectContext
    contextobj.Refresh(RefreshMode.StoreWins, context.myobjs.Local)
    contextobj.Refresh(RefreshMode.StoreWins, context.mychilds.Local)



    Is there anything wrong with this method ?

    • Edited by dcode25 Thursday, April 4, 2019 12:31 PM
    Thursday, April 4, 2019 10:52 AM
  • Hi dcode25,

    I think an effective way to solve slow program execution is to use asynchronous and multi-threading.

    Regards,

    Kyle


    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.

    Friday, April 26, 2019 6:52 AM