Search code examples
c#wpfobservablecollectionobjectcontext

Best way to keep ObservableCollection and ObjectContext in sync?


I have window with a listbox bound to an ObservableCollection of People (a set of entity framework objects that I retrieve in response to a users query: a search box), i then have functions such as Edit, Delete and Add New. At the moment i am simply making sure that each time i Add or Remove something from the Database that i also work with the OC. Is there a better way of handling this?

Thanks, Kohan.


Solution

  • I found that you may need to manage the OC youself when it comes to EF manipulations. For example, when you Add a new item to DB

    private bool AddItems(Item item)
    {
        bool addSucceed = false;
    
        // Do adding ...
    
        if(addSucceed)
            MyObservableCollection.Remove(item)
        else
            // Error notificaiton here.
    }
    

    Hope that helps.