locked
Losing Original values after executing Attach and Entity = State.Modified RRS feed

  • Question

  • Hi all,

    I am losing my Original values when using the code below:

            public virtual void Update(TEntity entityToUpdate)
            {
    	    //Here I can see the Original values
                PrintValues(context.Entry(entityToUpdate).OriginalValues);
                PrintValues(context.Entry(entityToUpdate).CurrentValues);
    
                dbSet.Attach(entityToUpdate);
                context.Entry(entityToUpdate).State = EntityState.Modified;
    
                //Here the Original values are gone. They are the same as the Current values
                PrintValues(context.Entry(entityToUpdate).OriginalValues);
                PrintValues(context.Entry(entityToUpdate).CurrentValues);
            }
    
    
        [HttpPost]
        public ActionResult _UpdateAjax(Guid id)
        {
          SccmSite sccmSite = unitOfWork.SccmSiteRepository.GetByID(id); //values from database
    
          try
          {
              if (TryUpdateModel(sccmSite))
              {
                  unitOfWork.SccmSiteRepository.Update(sccmSite); //calling method Update above
                  unitOfWork.Save();
    
                  return View(new GridModel(unitOfWork.SccmSiteRepository.Get()));
              }
          }
    ...
    

     

    on my DbContext:
    
        public override int SaveChanges()
        {
            //Original values will never get here
            return base.SaveChanges();
        }
    

    The problem is that I want to capture all the audit info during the SaveChanges() but Original values are gone by then.

     Update:

    If I use GetDatabaseValues() it works fine....
    Anyways... I have seen a lot people doing Audit using Current and Original values only without the need of using GetDatabaseValues()

    Not sure what I am missing...

    Any idea? Thanks


    Max
    • Edited by Maximusdm Thursday, January 12, 2012 10:34 PM
    Thursday, January 12, 2012 6:09 PM

Answers

  • Hi Max,

    Glad to see you again!

    I'd like to share some information about Attach method with you:

    -----------------------------------------------

    Adds an object to the ObjectContext and sets the object to the Unchanged state. In the Unchanged state, the Entity Framework treats the entity key values as final. If more than one entity of a particular type has the same key value, the Entity Framework will throw an exception. To avoid getting the exception, use the AddObject method to attach the detached objects and then change the state appropriately. http://msdn.microsoft.com/en-us/library/bb896271.aspx

    -----------------------------------------------

    It means the entity couldn't be in Dbcontext before attach operation. so the Attach method will write the Originalvalues.

    I think you can try to test the following code:

    using (var db= new EFTestContext())
                {
                    var e = new Employee() { EmployeeId = 1, Email = "attach", ModifyTime = DateTime.Now, Name1 = "attach", Phone = "attach" };
                    //db.Employees.Attach(e);
                    var employee = db.Employees.Find(1);
    
                    db.Entry(employee).CurrentValues.SetValues(e);
                    PrintValues(db.Entry(employee).OriginalValues);
                    PrintValues(db.Entry(employee).CurrentValues);
    
                }
    
    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 Maximusdm Friday, January 13, 2012 9:53 PM
    Friday, January 13, 2012 6:22 AM

All replies

  • Hi Max,

    Glad to see you again!

    I'd like to share some information about Attach method with you:

    -----------------------------------------------

    Adds an object to the ObjectContext and sets the object to the Unchanged state. In the Unchanged state, the Entity Framework treats the entity key values as final. If more than one entity of a particular type has the same key value, the Entity Framework will throw an exception. To avoid getting the exception, use the AddObject method to attach the detached objects and then change the state appropriately. http://msdn.microsoft.com/en-us/library/bb896271.aspx

    -----------------------------------------------

    It means the entity couldn't be in Dbcontext before attach operation. so the Attach method will write the Originalvalues.

    I think you can try to test the following code:

    using (var db= new EFTestContext())
                {
                    var e = new Employee() { EmployeeId = 1, Email = "attach", ModifyTime = DateTime.Now, Name1 = "attach", Phone = "attach" };
                    //db.Employees.Attach(e);
                    var employee = db.Employees.Find(1);
    
                    db.Entry(employee).CurrentValues.SetValues(e);
                    PrintValues(db.Entry(employee).OriginalValues);
                    PrintValues(db.Entry(employee).CurrentValues);
    
                }
    
    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 Maximusdm Friday, January 13, 2012 9:53 PM
    Friday, January 13, 2012 6:22 AM
  • Commenting out the "Attach" line fixed the problem.

    Thank you!

     


    Max
    Friday, January 13, 2012 9:53 PM