locked
Issue with ChangeTracker of EF6 in MVC6 RRS feed

  • Question

  • User-635033152 posted

    I am trying to overirde SaveChanges() method of DBContext in my MVC6 application with EF6. My requirement is to track the modified property of all entities and log in database.

    The problem is that originalValue  and currentValue both gives me updated value. I can't get the old values. Please suggest what I am doing wrong. I referred below link.

    http://www.exceptionnotfound.net/entity-change-tracking-using-dbcontext-in-entity-framework-6/

    public override int SaveChanges()
    {
    var modifiedEntities = ChangeTracker.Entries()
    .Where(p => p.State == EntityState.Modified).ToList();

    var now = DateTime.UtcNow;

    List<ChangeLog> ChangeLogs = new List<ChangeLog>();

    foreach (var change in modifiedEntities)
    {
    var entityName = change.Entity.GetType().Name;
    var primaryKey = GetPrimaryKeyValue(change);

    foreach (var prop in change.OriginalValues.PropertyNames)
    {
    var originalValue = change.OriginalValues[prop].ToString();
    var currentValue = change.CurrentValues[prop].ToString();
    if (originalValue != currentValue)
    {
    //Save log
    }
    }
    }
    return base.SaveChanges();
    }

    Monday, April 4, 2016 10:25 PM

Answers

  • User1559292362 posted

    Hi Abhimanyuk,

    Abhimanyuk

    I got the reason of this problem, but I have a follow up question now.

    I am glad to know you got the reason of this problem and thanks for sharing the solution. 

    It will be very beneficial for other community members who have the similar questions. 

    This can be very time consuming and inefficient process for complex models and at the same time I loose the benefit of single line code for saving objects through EF. Please suggest what is right approach.

    According to your description, you couldn't retrieve original value from your page, so you need to fetch the original value by using dbcontext with method named find, which entity framework could track of current and original values of the teacher entity. if you could retrieve original value, you could use Attach method instead of fetching it from database.

    for more information, please refer to:

    http://www.entityframeworktutorial.net/update-entity-in-entity-framework.aspx

    Best regards,

    Cole Wu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 13, 2016 1:52 AM

All replies

  • User-635033152 posted

    I got the reason of this problem, but I have a follow up question now.

    Earlier, the code in my Edit Action of Controller was


    public ActionResult Edit([Bind(Include = "TeacherId,TeacherName,TeacherAge,TeacherAddress")] Teacher teacher)



    db.Entry(teacher).State = EntityState.Modified;

    db.SaveChanges();

    return View(teacher);

    }

    If I change the code like below then it works and I am able to compare the old and new value, but in this case I have to explicitly set all the properties of my model. This can be very time consuming and inefficient process for complex models and at the same time I loose the benefit of single line code for saving objects through EF. Please suggest what is right approach.

    public ActionResult Edit([Bind(Include = "TeacherId,TeacherName,TeacherAge,TeacherAddress")] Teacher teacher)



    if (ModelState.IsValid)

    {

    Teacher dbTeacher = db.Teachers.Find(teacher.TeacherID);

    if (dbTeacher != null)

    {

    dbTeacher.TeacherName = teacher.TeacherName;

    dbTeacher.TeacherAge = teacher.TeacherAge;

    dbTeacher.TeacherAddress = teacher.TeacherAddress;

    db.SaveChanges();

    }

    }

    return View(teacher);
    }

    Wednesday, April 6, 2016 3:43 PM
  • User1559292362 posted

    Hi Abhimanyuk,

    Abhimanyuk

    I got the reason of this problem, but I have a follow up question now.

    I am glad to know you got the reason of this problem and thanks for sharing the solution. 

    It will be very beneficial for other community members who have the similar questions. 

    This can be very time consuming and inefficient process for complex models and at the same time I loose the benefit of single line code for saving objects through EF. Please suggest what is right approach.

    According to your description, you couldn't retrieve original value from your page, so you need to fetch the original value by using dbcontext with method named find, which entity framework could track of current and original values of the teacher entity. if you could retrieve original value, you could use Attach method instead of fetching it from database.

    for more information, please refer to:

    http://www.entityframeworktutorial.net/update-entity-in-entity-framework.aspx

    Best regards,

    Cole Wu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 13, 2016 1:52 AM