Base solution for your next web application
Open Closed

Entity History Helper #12324


User avatar
0
uenlkr4e created

Hi, I have this CustomerCredit entity that i am tracking.

public static readonly Type[] TenantSideTrackedTypes =
        [
            typeof(OrganizationUnit), typeof(Role), typeof(TenantCredit), typeof(CustomerCredit), typeof(Envelope)
        ];

When i call and update it using normal methods I can see the history generating. But i have this one method that runs inside a background jobs ExecuteAsync method and it updates the CustomerCredit entity also. The changes are reflected to db but nothing is created in history. It is like no property has been changed at all where i can see the changes in db.

My DropCredit method is in my CustomerManager class which basically implements MyAppDomainServiceBase. Am i missing something here? Here is my DropCredit method:

public async Task DropCredit(int customerId, string reason, int amount = 1)
    {
        var activeCredits = await _customerCreditRepository
            .GetAll()
            .Where(x => x.CustomerId == customerId && x.IsActive && x.CreditStartDate <= DateTime.Now && x.CreditEndDate >= DateTime.Now)
            .OrderBy(x => x.CreditEndDate)
            .ToListAsync();
        
        if (activeCredits.Count == 0) throw new UserFriendlyException(L("InsufficientCredit"));
        
        var activeCreditWithPositiveBalance = activeCredits.FirstOrDefault(x => x.RemainingCredit > 0);
        if (activeCreditWithPositiveBalance == null)
        {
            var creditToDrop = activeCredits.OrderByDescending(x => x.CreditEndDate).First();
            creditToDrop.RemainingCredit -= amount;
            creditToDrop.ModificationReason = reason;
            **await _customerCreditRepository.UpdateAsync(creditToDrop);**
        }
        else
        {
            activeCreditWithPositiveBalance.RemainingCredit -= amount;
            activeCreditWithPositiveBalance.ModificationReason = reason;
            **await _customerCreditRepository.UpdateAsync(activeCreditWithPositiveBalance);  **  
        }
    }

As you could see I call the update over customer credit and again i see the change in db so i know it updates. Any reason why history wont be created related to this update?

thanks

Markdown is supported
Copy & paste or drag & drop images (max 30 MB per image)

6 Answer(s)
  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi @uenlkr4e

    Since background jobs do not run within the default Unit of Work context the changes are saved to the database but the Entity History Tracking mechanism is not triggered. Can you add [UnitOfWork] attribute to the ExecuteAsync method in your BackgroundJobs class and check if the problem is solved?

    Or

    public class DropCustomerCreditJob : AsyncBackgroundJob< DropCustomerCreditJobArgs >, ITransientDependency
    {
        private readonly ICustomerManager _customerManager;
        private readonly IUnitOfWorkManager _unitOfWorkManager;
    
        public DropCustomerCreditJob(ICustomerManager customerManager, IUnitOfWorkManager unitOfWorkManager)
        {
            _customerManager = customerManager;
            _unitOfWorkManager = unitOfWorkManager;
        }
    
        public override async Task ExecuteAsync(DropCustomerCreditJobArgs args)
        {
            using (var uow = _unitOfWorkManager.Begin())
            {
                await _customerManager.DropCredit(customerId, "Background job deduction");
                uow.Complete();
            }
        }
    }
    
    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    uenlkr4e created

    Hi, I already have the [UnitOfWork] attribute so it doesnt seem to work. I will give a shot to your second suggestion now.

    thanks

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    uenlkr4e created

    Hi @oguzhanagir unfortunately having the code inside using block didnt work either. The item is updated but the history is not created.

    any other suggestions?

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi @uenlkr4e

    We will check this situation

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    uenlkr4e created

    Hi @oguzhanagir, any updates on this?

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi @uenlkr4e

    EntityHistory checks if it is enabled with the following conditions: whether it is globally enabled in the EntityHistoryConfiguration and whether there is a user in the AbpSession, or if the IsEnabledForAnonymousUsers setting is enabled. It evaluates these conditions to return the change set.

    If the user is not logged in, you can inject IAbpSession and use the Use() method to set the user, or you can enable the IsEnabledForAnonymousUsers setting. In this case, Entity History must be globally enabled in the EntityHistoryConfiguration.

    Could you check these conditions and see if the issue still persists?

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)