locked
When adding entities using AddToXXX the context collection does not contain the new item RRS feed

  • Question

  • Hi,

    Just found a small issue with adding new items to an Entity Collection. If I was to write the following code:

     

    _dataContext.AddToOrders(new Order());
    
    foreach(Order o in _dataContext.Orders)
    {
    }
    

     

    Then I would find that the new order is NOT inside _dataContext.Orders unless I call _dataContext.SaveChanges(). How can I make sure this collection is updated when adding new items without committing to the server?

    Regards,

    Neil

    Thursday, August 5, 2010 12:43 PM

Answers

  • By doing foreach (Order o in _dataContext.Orders), you are actually executing an query and walking over the results returned by the server. Since the object you have added haven't been saved yet, it would be weird to return the unsaved order in this list.

    Does this make sense?

    Thanks

    Pratik


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Monday, August 9, 2010 5:31 PM
    Moderator