Search code examples
aspnetboilerplateabp-framework

BackgroundJobs do not let you get Users with IRepository


Use case: I need to pull a user record based on the ARGS coming from the BackgroundJob

public class ULBackgroundJob : BackgroundJob<ULJobArgsDto>, ITransientDependency, IULBackgroundJob
{
 private readonly IRepository<User, long> _userRepository;
 public ULBackgroundJob
 (IRepository<User, long> userRepository)
  {
    _userRepository = userRepository;
  }

 public  override void Execute(ULJobArgsDto args)
 {
  User user = _userRepository.FirstOrDefault(args.UserId);
 }
}

Results: I always get zero results and I have checked that the user id value exists.

Suspected Issue: The SQL that is generated inserts "@__ef_filter__IsMayHaveTenantFilterEnabled_1=1" into the query so I suspect I need to somehow get that set to Zero when I run from a BackgroundJob..?


Solution

  • You need to disable IMayHaveTenant filter to view cross-tenant entities:

    using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.MayHaveTenant))
    {
        User user = _userRepository.FirstOrDefault(args.UserId);
    }
    

    You can read more about data filters in the ASP.NET Boilerplate documentation: https://aspnetboilerplate.com/Pages/Documents/Data-Filters