Search for existing suggestions

DiscardChanges method

We need an easy way to give the user the "Cancel" functionallity when he make changes to entities data but he does not want to save changes pressing Cancel button. I think adding DiscardChanges to ObjectContext is the solution, going back to its initial state and dissapearing added entities (even child entities) and returning modified or deleted entities to their original state

42 votes
Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)

We’ll send you updates on this idea

José Flores shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

4 comments

Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
Submitting...
  • shimmy commented  ·   ·  Flag as inappropriate

    This looks like the optimal solution when using a singleton or long running DbContext.
    However when working with short-lived contexts for example in ASP.NET apps, transactions is another good way to achieve this.

  • Julien N commented  ·   ·  Flag as inappropriate

    @Max, last time I tried, Refresh only does a partial job : it only refresh properties and not relationships.
    Moreover, added or deleted items are not removed or restored.

    Discarding the context is the "standard" solution, but it causes a lot of other issues, especially existing entities are now "contextless" and must be attached to the new context.

    On many projects I'd love to see this feature exist !

  • Max commented  ·   ·  Flag as inappropriate

    Jose, These line of code would partially do the job

    public void DiscardChanges()
    {
    Context.Refresh(RefreshMode.StoreWins, GetObjectSet());
    }

    private ObjectSet<T> GetObjectSet()
    {
    return Context.CreateObjectSet<T>();
    }

Feedback and Knowledge Base