السؤال ObjectContext.Load() does not refresh all changes Code First

  • Wednesday, September 19, 2012 10:12 AM
     
     
    Hello,

    I have this strange problem:

    You have a database with these rows:

    Table: Letter

    Id=1,Name="A"
    Id=1,Name="B"
    Id=1,Name="C"

    Now you make the query

    var q= (from l in context.Letter where l="A" select l);
    g.Load();

    Now in context.Local yo have only one row (the first one)

    If you go to database and remove or delete or add files, the Local refresh accordingly (you must use Load() and Refresh() between changes).

    But in this case it does not work, see it:

    You go to database and change "A" to "B" and repeat the query
    g.Load()

    The entity changes his value to "B" so it must been removed from Local but it stills alive in Local (count==1) when the local must have 0 elements (count==0)

    Any idea?

All Replies

  • Friday, September 21, 2012 2:56 AM
    Moderator
     
     

    Hi Eric,

    I have moved this thread to EF forum for better support.

    Thank you for your understanding and support.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • Monday, September 24, 2012 5:31 AM
    Moderator
     
     

    Hi Eric,

    Welcome to the MSDN forum.

    Could you please let me know the specific steps to reproduce your problem?

    I am not sure if you rerun the application after you change “A” to “B”.

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

  • Monday, September 24, 2012 11:02 AM
     
     

    Hello,

    Is not necessary rerun the application, I think that this behaviour is due to "MergeAs(AppendOnly)" option that i have seen via debugger, it seems an internal option that i can not change.

    The steps are.

    1 Make a Query

    2 Change a value in database that previously matches the query so can't match now.

    3 Local

    4 Refresh()

    After these two sentences, only new or deleted or not filtered items are included In Local, but not the already existing items.

    For example:

    Id=1,Name="A"
    Id=1,Name="B"
    Id=1,Name="C"

    You do a query for items with Name="A"

    Then you must change in Database "A" => "B"

    After that you use Load and Refresh() (via ObjectContext)

    Local will have one Element (the old "A" with "B" Name) when i expected to have 0 elements, because anyone matches the query.

    Thanks,



  • Monday, September 24, 2012 11:02 AM
     
     
    Thanks, i didn't find the EF forum.
  • Wednesday, September 26, 2012 9:34 AM
    Moderator
     
     

    Hi Eric,

    I have tested your problem via steps you provided. Although you refreshed the entity object, the query has not been executed twice. As the results, the entity object which the query returned will refresh its property values. In addition, after an entity object is attached to object context, this object will stay in memory until it is deleted or detached (Garbage collection can only occur if the user code does not have any references to the detached object). During the time (stay in memory, which means it is tracked by Object Context), if you refresh this object, it just updates its property values.

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

  • Wednesday, September 26, 2012 10:02 AM
     
     

    In some cases is necesary reload and refresh, one row changes and must be showed in application.

    I supposed that, the problem is that i have a user interface with a grid and if the user changes the filter, he will see invalid data, so the Local property in this scenario is not valid and maybe Local is only valid for static data, maybe the not common usual scenario.

  • Saturday, October 20, 2012 1:22 PM
     
     

    Hi Eric,

    Did youu find a solution as I'm encountering the same problem.