Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8873445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:28:01+00:00 2026-06-14T18:28:01+00:00

I’ve just read this question: How to cancel an edit to an object using

  • 0

I’ve just read this question:
How to cancel an edit to an object using MVVM?

I have the exact same question and would like to have a simple solution. The first one looked very promising, however, I’m using entity framework and my classes are automatically generated, so that’s not an option.

How can I do this easily with EF?

EDIT:
My ViewModel:

public List<Player> Players
{
    get { return repository.Players.OrderBy(x => x.Firstname).ToList(); }
}

public Player CurrentPlayer
{
    get { return currentPlayer;  }
    set
    {
        if (currentPlayer != value)
        {
            currentPlayer = value;
            RaisePropertyChanged("CurrentPlayer");
        }
    }
}

Players is bound to a datagrid, CurrentPlayer to the selecteditem of that. Below the datagrid, I have textboxes where the user can edit the player info.

When the user presses the save button, this code is executed:

private void SaveExecute(object parameter)
{
    repository.SavePlayer(currentPlayer);

    Editing = false;
}

Very easy. When the user presses the cancel button, this is executed:

private void CancelExecute(object parameter)
{
    if (currentPlayer.Id == 0) // id = 0 when a new player is being added
    {
        CurrentPlayer = null;
    }
    else
    {
        // here, the CurrentPlayer should be set back to it's previous state.
    }

    Editing = false;
}

CurrentPlayer is an object of Player, which is an entity class generated by EF.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:28:02+00:00Added an answer on June 14, 2026 at 6:28 pm

    I don’t understand the problem. If the user is editing a new item (State == ObjectState.Added) then you discard that, (and maybe set the CurrentPlayer to what it was before pressing the “New” Button?), else just retrieve the entity from the database again and that’s it…

    A better way to solve this problem is to have your CRUD and your List VMs have separate instances of the entity.

    For example, when I create a List view (Datagrid or otherwise), usually the data displayed in that is just a subset of the whole data displayed in the full CRUD View. So, in order to show the entity in the CRUD, I need to Get() the entity again using the necessary Includes. This resolves the whole cancel problem, because the entity instance you’re modifying is actually not the same as the one shown in the List view. If the user presses Save, you can replace the instance shown in the list view with the edited one, and if the user presses cancel, don’t do anything.

    Edit: Also be aware that if your entities are being generated by a T4 Template such as the Entity Framework STE Template, you can modify the .tt file and customize it to generate whatever code you need in your entities.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I would like to run a str_replace or preg_replace which looks for certain words
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I would like to count the length of a string with PHP. The string
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.