Base solution for your next web application
Open Closed

Save Entity History for only record update and delete but not for record creation #9376


User avatar
0
razkhan78 created

Hi

We are using AspNetZero (Paid) Product version --> 4.0 Product type --> MVC Product framework type --> .net core

We have 1)Enabled below lines to enable entity history Configuration.EntityHistory.IsEnabled = true;

2)/Uncomment below lines to write change logs for the entities: Configuration.EntityHistory.Selectors.Add("LoadStopEntities", EntityHistoryHelper.TrackedTypes); Configuration.CustomConfigProviders.Add(new EntityHistoryConfigProvider(Configuration));

Now in logs it creates entries for create record, update record and delete record.

But we need to disable history logging for create record action.

So we would like to know is there any class or interface we can implement to achive it?


3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You can create your own EntityHistoryHelper (for example MyEntityHistoryHelper) derived from https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.ZeroCore.EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs and override SaveAsync and Save methods. You can implement SaveAsync like below;

    public virtual async Task SaveAsync(EntityChangeSet changeSet)
    {
    	if (!IsEntityHistoryEnabled)
    	{
    		return;
    	}
    
    	if(entityChange.ChangeType == EntityChangeType.Created){
    		return;
    	}
    
    	UpdateChangeSet(changeSet);
    
    	if (changeSet.EntityChanges.Count == 0)
    	{
    		return;
    	}
    
    	using (var uow = UnitOfWorkManager.Begin(TransactionScopeOption.Suppress))
    	{
    		await EntityHistoryStore.SaveAsync(changeSet);
    		await uow.CompleteAsync();
    	}
    }
    

    Save will be similar but not async.

  • User Avatar
    0
    razkhan78 created

    We already have one class with same name src\Project.Core\EntityHistory\EntityHistoryHelper.cs , which is static class. so how do we handle it.

  • User Avatar
    0
    musa.demir created

    It is not have to be created with the same name. You should create a class which inherits https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.ZeroCore.EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs, then replace current implementation with yours.

    public class YOURModule : AbpModule
    {
    	public override void PreInitialize()
    	{
    		//...
    		Configuration.ReplaceService<IEntityHistoryHelper, MyEntityHistoryHelper>(DependencyLifeStyle.Transient);
    	}
    }