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
2 Answer(s)
-
0
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(); } } }
-
0
Hi, I already have the [UnitOfWork] attribute so it doesnt seem to work. I will give a shot to your second suggestion now.
thanks