locked
EF Needs to revert to Original Values RRS feed

  • Question

  • I need to implement a EditorViewModel<TEntity> that supports reset to orginal values and I'm not having any luck finding examples.

    I've go the stateEntry and am not quite sure how to use the DbDataRecord from .GetUpdatableOriginalValues() or what to pass .ApplyOriginalValues(?) 

    Wednesday, March 23, 2011 10:05 PM

Answers

  • Hi Ryan,

    Welcome!

    You can use SetValues in EF4.1RC:

    var blog = context.Blogs.Find(2);
            Blog b = new Blog() { BlogId = 2, BlogName = "heh" };
            context.Entry(blog).OriginalValues.SetValues(b);
            PrintValues(context.Entry(blog).OriginalValues);
            var dbblog = context.Entry(blog).GetDatabaseValues();
            context.Entry(blog).OriginalValues.SetValues(dbblog);
            PrintValues(context.Entry(blog).OriginalValues);
    
     public static void PrintValues(DbPropertyValues values)
        {
          foreach (var propertyName in values.PropertyNames)
          {
            Console.WriteLine("Property {0} has value {1}",
                     propertyName, values[propertyName]);
          }
        }
    

    You can refer here : http://blogs.msdn.com/b/adonet/archive/2011/01/30/using-dbcontext-in-ef-feature-ctp5-part-5-working-with-property-values.aspx

    Have a nice day.


    Alan Chen[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.

    • Marked as answer by Alan_chen Tuesday, April 5, 2011 2:46 AM
    Thursday, March 24, 2011 1:59 PM

All replies

  • Hi Ryan,

    Welcome!

    You can use SetValues in EF4.1RC:

    var blog = context.Blogs.Find(2);
            Blog b = new Blog() { BlogId = 2, BlogName = "heh" };
            context.Entry(blog).OriginalValues.SetValues(b);
            PrintValues(context.Entry(blog).OriginalValues);
            var dbblog = context.Entry(blog).GetDatabaseValues();
            context.Entry(blog).OriginalValues.SetValues(dbblog);
            PrintValues(context.Entry(blog).OriginalValues);
    
     public static void PrintValues(DbPropertyValues values)
        {
          foreach (var propertyName in values.PropertyNames)
          {
            Console.WriteLine("Property {0} has value {1}",
                     propertyName, values[propertyName]);
          }
        }
    

    You can refer here : http://blogs.msdn.com/b/adonet/archive/2011/01/30/using-dbcontext-in-ef-feature-ctp5-part-5-working-with-property-values.aspx

    Have a nice day.


    Alan Chen[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.

    • Marked as answer by Alan_chen Tuesday, April 5, 2011 2:46 AM
    Thursday, March 24, 2011 1:59 PM
  • Hi Ryan,

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions? 
     
    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[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.

    Tuesday, March 29, 2011 2:41 PM