I'm trying to track the changes made to an entity of my plugin, however I'm getting null when I use the method LoadOriginalCopy() of the DbContextExtensions class.
The method (Nop.Data/Extensions/DbContextExtensions.cs):
public static TEntity LoadOriginalCopy<TEntity>(this IDbContext context, TEntity entity) where TEntity : BaseEntity
{
return LoadEntityCopy(context, entity, entityEntry => entityEntry.OriginalValues);
}
private static TEntity LoadEntityCopy<TEntity>(IDbContext context, TEntity entity, Func<EntityEntry<TEntity>, PropertyValues> getValuesFunction) where TEntity : BaseEntity
{
if (context == null)
throw new ArgumentNullException(nameof(context));
//try to get the EF database context
if (!(context is DbContext dbContext))
throw new InvalidOperationException("Context does not support operation");
//try to get the entity tracking object
var entityEntry = dbContext.ChangeTracker.Entries<TEntity>().FirstOrDefault(entry => entry.Entity == entity);
if (entityEntry == null)
return null;
//get a copy of the entity
var entityCopy = getValuesFunction(entityEntry)?.ToObject() as TEntity;
return entityCopy;
}
This is how I'm calling the method from some controller in my plugin:
MyEntity entity = model.ToEntity<MyEntity >();
entity .CreatedOnUtc = DateTime.UtcNow;
//Get original values
var originalValues = _dbContext.LoadOriginalCopy(entity);
where _dbContext refers to my plugin's own dbcontext:
public class PluginObjectContext : DbContext, IDbContext { ... }
and it's registered in the DependencyRegistrar class as below:
builder.RegisterPluginDataContext<PluginObjectContext>(PLUGIN_OBJECT_CONTEXT);
Could anyone help me figure out how to make this work?