none
How to refresh my whole ObjectContext from my data source (SQL SERVER). RRS feed

  • Question

  • Hello All,

    My application is running in multiple user environment, where I use EF 4.0 for database activity. my requirement is I want to Refresh my Context at any point of time which will give me all latest data present in database but my changes should not get overritten. I tried with following code

    Context.Refresh(RefreshMode.ClientWins,
    Context.ObjectStateManager.GetObjectStateEntries(EntityState.Added |EntityState.Deleted | EntityState.Modified |EntityState.Unchanged));

    But it Throws Exception as follows:

    Please help me with some valuable solution.

    Regards
    Debasish


    Debasish Das Software Developer. gloStream Inc.


    • Edited by Debasish Das Tuesday, May 21, 2013 10:35 AM Text Formatted
    Tuesday, May 21, 2013 10:34 AM

Answers

  • Hi Debasish,

    ObjectContext.ObjectStateManeger.GetObjectStateEntries() method returns a collection of ObjectStateEntry objects. However I think you should pass the entity objects to the ObjectContext.Refresh() method.

    You can try the following code to get the entities from the entries:

    Context.Refresh(RefreshMode.ClientWins,
     Context.ObjectStateManager.GetObjectStateEntries(EntityState.Added |EntityState.Deleted | EntityState.Modified |EntityState.Unchanged).Select(e=>e.Entity));
    Best regards,

    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 22, 2013 7:29 AM
    Moderator

All replies

  • Hello All,

    My application is running in multiple user environment, where I use EF 4.0 for database activity. my requirement is I want to Refresh my Context at any point of time which will give me all latest data present in database but my changes should not get overritten. I tried with following code

    Context.Refresh(RefreshMode.ClientWins,
    Context.ObjectStateManager.GetObjectStateEntries(EntityState.Added |EntityState.Deleted | EntityState.Modified |EntityState.Unchanged));

    But it Throws Exception as follows:

    Please help me with some valuable solution.

    Regards
    Debasish


    Debasish Das Software Developer. gloStream Inc.

    Tuesday, May 21, 2013 10:37 AM
  • Hi Debasish,

    ObjectContext.ObjectStateManeger.GetObjectStateEntries() method returns a collection of ObjectStateEntry objects. However I think you should pass the entity objects to the ObjectContext.Refresh() method.

    You can try the following code to get the entities from the entries:

    Context.Refresh(RefreshMode.ClientWins,
     Context.ObjectStateManager.GetObjectStateEntries(EntityState.Added |EntityState.Deleted | EntityState.Modified |EntityState.Unchanged).Select(e=>e.Entity));
    Best regards,

    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 22, 2013 7:29 AM
    Moderator
  • Hello Chester,

    Thank you for your Reply. I tried your code, it runs without exception,and it works only for Entity exists in my current context, but don't update the new entity's added or deleted, added and deleted records of a table are not reflecting in my context after refresh . can you please come up with few more solutions. I will be thankful to you.

    Regards
    Debasish


    Debasish Das Software Developer. gloStream Inc.


    Wednesday, May 22, 2013 9:49 AM
  • Hi Debasish,

    I think if the entity is added, there isn't the information of this entity in the store. I'm not sure what exactly is the problem. Do you have any examples about what is the expected result and what is the actual result?

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 23, 2013 9:11 AM
    Moderator
  • Hello Chester,

    for example I have one table in the database, which is included in the context.

    Suppose there are two users working in the application in parallel. User 1 added few new records into the table, and at certain point of time user 2 wants to have all those newly added records inserted by user 1 into the existing context of user 2 (for maintaining concurrency). this is my main scenario. for this I tried the above suggested code, but it refresh only the existing records (Unchanged entity) of user 2, but not updating the context of user 2 with all deleted or added records by user 1.

    I think the scenario is pretty clear for you. please give me some solution.

    RegardsDebasish 


    Debasish Das Software Developer. gloStream Inc.

    Friday, May 24, 2013 5:32 AM
  • Hi Debasish,

    Thanks for the information.

    Is context.SaveChanged() method called in user1's context? I think if the added entity exists only in user1's context, it won't be traced by GetObjectStateEntries() method in user2's context.

    Base on my understanding, using Refresh method with ClientWins will execute a query against the database and save the current server values into original values of the entity.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 27, 2013 3:04 AM
    Moderator