locked
Refreshing the values in a disconnected entity... RRS feed

  • Question

  • WPF/MVVM/EF/.Net4.0

    When a user clicks to edit an item in my TreeView, I want to refresh the values in the entity in the underlying ViewModel before presenting the details screen.  I have some code that seems to work for updating the values, but it also seems to break the binding:

    public virtual void Refresh(params T[] items)
    {
    	using (var context = new Entities())
    	{
    		DbSet<T> dbSet = context.Set<T>();
    
    		foreach (T item in items)
    		{
    			dbSet.Add(item);
    			foreach (DbEntityEntry<IEntity> entry in context.ChangeTracker.Entries<IEntity>())
    			{
    				IEntity entity = entry.Entity;
    				entry.State = GetEntityState(entity.EntityState);
    			}
    		}
    
    		context.ToObjectContext().Refresh(System.Data.Objects.RefreshMode.StoreWins, items);
    	}
    }

    In this case the entity is a "Plan" with a populated navigation property of the "PlanType".  In the view, a ComboBox is bound to the PlanType.

    I want to make sure that the user is presented with the most current data when they attempt to edit.  Initially, everything works just fine...  A user clicks the edit button on the TreeView node, the refresh method is called on the entity in the ViewModel and the details screen pops up with the appropriate value reflected in the ComboBox.  Then I change the PlanType reference directly in the database to simulate a change from another user.  When I edit the same node, this time my combobox comes up blank instead of reflecting the new PlanType.  It seems that the binding is losing its context after the second call to Refresh.  When I trace the code, it looks like all the values are in place.  Is the original object getting replaced with a new instance, somehow?

    Any insight here is greatly appreciated.


    http://digitalcamel.blogspot.com/


    Wednesday, October 9, 2013 6:29 AM

Answers

  • Hello,

    Welcome to this forum.

    With your codes, I did a test and found that just as what you said that all the values are in place.

    Below is my test codes:

    internal void Execute()
    
            {
    
                Order[] orders = new Order[1] { new Order() { OrderID = 1, OrderCode = "0", OrderName = "0" } };
    
    
                Refresh(orders);
    
                orders[0].OrderCode = "1";
    
                orders[0].OrderName = "1";
    
    
                Refresh(orders);
    
            }
    
    
            public virtual void Refresh(params Order[] items)
    
            {
    
                using (DataBaseFirstDBEntities context = new DataBaseFirstDBEntities())
    
                {
    
                    DbSet<Order> dbSet = context.Set<Order>();
    
    
                    foreach (Order item in items)
    
                    {
    
                        dbSet.Add(item);
    
                        context.Entry(item).State = EntityState.Modified;
    
                    }
    
                    var objectContext = ((IObjectContextAdapter)context).ObjectContext;
    
                    objectContext.Refresh(System.Data.Objects.RefreshMode.StoreWins, items);
    
                }
    
            }

    I called the Refresh() method twice, and both would refresh the Order data from database.

    It seems that it causes by the banding codes and for this I recommend you to post a thread regarding this to the WPF forum.

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=wpf

    Or you can upload your program to skydirve and share it with us so that we can involve WPF experts to help us. 

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Fred Bao Thursday, October 10, 2013 3:17 AM Better
    • Marked as answer by Fred Bao Wednesday, October 16, 2013 9:25 AM
    Thursday, October 10, 2013 2:22 AM