none
Why is there so much basic functionality missing from EF RRS feed

  • Question

  • I've been using EF for a few months now and I am finding works pretty well but it is missing some REALLY basic features that I would have though it would support. For example:

    - can't undo all unsaved changes.

    - can't undo single objects

    - can't save a single object

    - have to pass the context around when it would appear the objects themselves somehow can get to the context (for lazy loading)

    - no good way to handle popup windows were a user makes some changes and then cancels.

    Opinions?

     

    Thursday, July 28, 2011 6:13 AM

All replies

  • Hello MickleKulls,

    Welcome to EF forum.

    You can use the ObjectStateEntry's original values and current values records. In addition, your entity need to implement IEntityWithKey. http://msdn.microsoft.com/en-us/library/bb738667(v=VS.90).aspx

    In addition, I suggest you can check this thread maybe helpful for you. http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/c12bd8c8-231f-4dcc-a665-b048f7debbd7

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, August 1, 2011 3:29 AM
  • Hello MickleKulls,

    Welcome to EF forum.

    You can use the ObjectStateEntry's original values and current values records. In addition, your entity need to implement IEntityWithKey. http://msdn.microsoft.com/en-us/library/bb738667(v=VS.90).aspx

    In addition, I suggest you can check this thread maybe helpful for you. http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/c12bd8c8-231f-4dcc-a665-b048f7debbd7

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Hi Larcolais,

    Thanks for the reply. I actually found that second post a few weeks ago and have been using th undo code they suggested with good sucess. My question though was "why isn't this functionality built in?". I guess the only answer to that there is not enough staff assigned to that department. It seems that every post I read from the team ends with some comment about time constraints. Being this is MS, can't we just get more staff? :-)

    Wednesday, August 3, 2011 12:11 AM
  • PS. I am available ;-)
    Wednesday, August 3, 2011 12:11 AM
  • Hello again,

    If you want to undo before SaveChanges method, you can check TransactionScope class. Check it here. http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx But, this also let you point it out where the program needs to rollback before it was committed to database side. In addition, perhaps you can describe the background of your need and we can analyze the specific need and provide more helpful work arounds to you.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, August 3, 2011 7:29 AM
  • Hello again,

    If you want to undo before SaveChanges method, you can check TransactionScope class. Check it here. http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx But, this also let you point it out where the program needs to rollback before it was committed to database side. In addition, perhaps you can describe the background of your need and we can analyze the specific need and provide more helpful work arounds to you.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    As far as undo goes I have 2 specific needs. They are both fairly similar so could be considered the one issue.

    The first is if there are any errors when saving I would like to roll back all changes. My program save changes frequently without asking the user so the only changes are generally what they have just done. For example, they might have a window that lists Employees. They click an add button to add an employee and a popup window appears. That popup window creates an employee object when it is loaded but it remains detached (this works well for data binding). They enter details into that window and push ok. The new Employee object is added to the context and save is called but they get an error. In this case the window remains open and I roll back any changes that have been made. Note that the changes might be complex if adding an employee also causes other objects to be created. Currently I just reload all the data.

     

    The second case is if they click to edit an object and a popup window appears, they make changes but push cancel.

     

    I think both of these cases are a fairly common requirement for pretty much all users of EF. Currently I either undo my changes manually if the changes are simple or reload all the data. The first method isn't perfect because I get it wrong then the user will always get errors for every save and not know why, resulting in them having to close the window and reopen. The second isn't perfect because I might have several different collections in memory and need to reload them all and redo binding etc.

    Thursday, August 4, 2011 12:25 AM
  • In EF, SaveChanges operates within a transaction. It will roll back that transaction and throw an exception if any of the dirty ObjectStateEntry objects cannot be persisted. This was a convenient way to achieve rollback feature before it was saved to database side. check this document. http://msdn.microsoft.com/en-us/library/bb336792.aspx

    Thanks,

    Werewolf,


    Just a newbie for everything.

    • Proposed as answer by Werewolfs Thursday, August 4, 2011 11:12 AM
    Thursday, August 4, 2011 11:11 AM
  • In EF, SaveChanges operates within a transaction. It will roll back that transaction and throw an exception if any of the dirty ObjectStateEntry objects cannot be persisted. This was a convenient way to achieve rollback feature before it was saved to database side. check this document. http://msdn.microsoft.com/en-us/library/bb336792.aspx

    Thanks,

    Werewolf,

     


     

    Just a newbie for everything.


    I am well aware of that but I want to roll back the changes I have made to the EF, not what changes EF has made to the database. eg

    entities.AddSomething(something);
    entities.AddSomething(somethingelse);
    etc
    try
    {
    entities.SaveChanges();
    }
    catch
    {
    entities.DiscardEverythingIHaveDOne();  //<---- this is what I need
    }

    Thursday, August 4, 2011 11:52 PM
  • I hate to bring this thread back up but I am getting quite frustrated here. There simply is no good consistent way that you can work with the entity framework without having to write hacky extension methods using undocumented methods that work in some case but not others. It's just not possible to write code and use the same techniques in all places. In some cases I need to call undo on a single object, in other cases I have to remove objects or add objects back. In other cases I just create a new context and reload everything. The framework really desperately needs the following features

    ability to clone an entity (for popup windows)
    ability to apply the cloned object back into the main context but revert that if it fails to save.
    ability to undo changes to a single object
    ability to undo all changes without going to the database
    ability to save the state (either full state or a single object) and then roll back to that state

    It seems to me that the team is working on new fancy features such as code first yet the basic functionality that is desperately needed is being ignored. What is going on here?


    • Edited by MickleKulls Friday, September 2, 2011 6:11 AM
    Friday, September 2, 2011 6:10 AM