locked
How to implement Scalar Change tracking for POCO RRS feed

  • Question

  • I have an MVC 3 solution, using the Entity Framework 4.3.  The Entity Framework is using the ObjectContext and the database first approach.

    I have a requirement to implement an Audit trail and be able to present a user with a list of changes on a complex object and recreate the object at any point in time, showing the changes.  I have populated my audit tables using update, delete, insert triggers on the required tables; this works well for my purposes.

    The problem is that the process for my updates are a user views a screen, makes some changes, and clicks next to get to the next screen.  Clicking next effectively performs a post to the EDIT action, followed by a redirect to the next page. 

    Here is my code for the edit action...

            [HttpPost]
            public virtual ActionResult Edit(int Id, FormCollection entity)
            {
                try
                {
                    if (!ModelState.IsValid)
                    {
                        return Json(new
                        {
                            success = false,
                            errors = GetErrorsFromModelState(ModelState)
                        });
                    }
    
                    if (Id == 0)
                    {
                        var newEntity = new Plan();
                        TryUpdateModel(newEntity);
                        service.AddEntity(newEntity);
                    }
                    else
                    {
                        var updatedEntity = service.Get(Id);
                        TryUpdateModel(updatedEntity);
                        service.UpdateEntity(updatedEntity)
                    }
                    
                    return Json(new
                    {
                        success = true
                    });
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError(ex.Message, ex);
    
                    return Json(new
                    {
                        success = false,
                        errors = GetErrorsFromModelState(modelState)
                    });
                }
            }

    Service.UpdateEntity simply passes the request to the repository...

    public void UpdateEntity(IEntity entity)
    {
        repository.UpdateEntity(entity);
    }

    Repository.UpdateEntity updates the datacontext (Objectcontext)...

    public virtual void Update(T entity)
    {
        string entitySetName = string.Format("{0}.{1}", Dbset.Context.DefaultContainerName, Dbset.EntitySet.Name);
    
        oc.ApplyCurrentValues(entitySetName, entity);
        oc.SaveChanges();
    }

    The problem is that even if nothing is changed on the screen the entity framework still marks all properties as modified, so every page view is creating an audit entry. I cant find any way to implement scalar change tracing on the entities.

    I have one possible solution, to put a next AND update button on the page, with the update posting to the edit and the next simply redirecting to the next page in the nav path.  Then using jquery hide the update button and serialise my form when it is loaded.  when the user clicks next serialise the form compare before and after, and call the update if required.  Seems like it will work, unless javascript is disabled. 

    I would really like a way to do this in code?  Is there a way to NOT get the EF Model to be modified if the user doesnt make any changes on screen?

    Thanks. Jeff

    Tuesday, April 17, 2012 8:32 AM

All replies

  • Hi Jeff,

    Welcome!

    We will do some more pending research  about your problem and come back as
    soon as possible, Thanks for understanding.

    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.

    Wednesday, April 18, 2012 1:15 PM