Base solution for your next web application
Open Closed

It can't get the entry's OriginalValues with ChangeTracker! #1011


User avatar
0
winson created

hi, I want to do some auditlog for the field values changed with below:

foreach (var entry in ChangeTracker.Entries<FormDataDetail>().Where(a => a.State == EntityState.Modified))
{    
    if (entry.CurrentValues["FieldValue"].ToString() != entry.OriginalValues["FieldValue"].ToString())
    {
       //to do some auditlog
    }
}

but the problem is the OriginalValues always same with the current value, and after I review the ABP source code, I find the below code in update function:

public override TEntity Update(TEntity entity)
        {
            AttachIfNot(entity);
            Context.Entry(entity).State = EntityState.Modified;
            return entity;
        }

I think the problem is

Context.Entry(entity).State = EntityState.Modified;

for the detail, you can take a look this article.

so can we fix this issue?

for now, I have to get the OriginalValues from db again, like below:

var oldValue = entry.GetDatabaseValues().GetValue<string>("FieldValue");

thanks!


5 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Do not use Update. Just query your entity from database (repository.get(ID) for example) and change it's properties. It will be updated, no need to call Update method.

    Then override SaveChanges (<a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/src/Abp.EntityFramework/EntityFramework/AbpDbContext.cs#L134">https://github.com/aspnetboilerplate/as ... xt.cs#L134</a>) and SaveChangesAsync in your DbContext, get original values and call base savechanges/savechangesasync.

    This may work.

  • User Avatar
    0
    winson created

    oh, that's great! thanks!!

  • User Avatar
    0
    winson created

    Hi, I just tried this these days, but seems doesn't work, if I didn't call the update method, and the record can't be update, I also try to add the UnitOfWork Attribute but still doesn't work, and support my service should not need to add the UnitOfWork attribute, right? my service class has been inherit the applicationService and IApplicationService

  • User Avatar
    0
    winson created

    If don't use Update method, I just can use the below code for update the data with unitOfWork:

    using (var unitOfWork = _unitOfWorkManager.Begin())
    {
         ....
    }
    
  • User Avatar
    0
    winson created

    oh, sorry, I found the issue, for unitOfWork must be use the virtual method!!

    thanks!