Base solution for your next web application
Open Closed

IRepository not working as expected in background job #2894


User avatar
0
yrojas created

Hi there, Im using IRepository<OrganizationUnit, long> in a schedule background job with Quartz, but the getAll() method is returning empty, even when I have organization units in my database. The repository works fine inside managers and services.

This is part of my code:

public class MyBackgroundJob : JobBase, ITransientDependency
    {
         private readonly IRepository<OrganizationUnit, long> _organizationUnitRepository;

        public MyBackgroundJob(IRepository<OrganizationUnit, long> organizationUnitRepository)
        {
                 _organizationUnitRepository = organizationUnitRepository;
        }

        public override void Execute(IJobExecutionContext context)
        {
            using (var unitOfWork = UnitOfWorkManager.Begin())
            {
                IQueryable<OrganizationUnit> organizationUnits = _organizationUnitRepository.GetAll();
                .....

At this point organizationUnits its allways empty.

What can I be missing here?

Thanks in advance.


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

    Hi,

    You query will be executed for host records with TenantId=null. Maybe that is why you don't get any records.

    In background jobs, you need to explicitly set tenantId you wnat to operate on. See <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Multi-Tenancy#switching-between-host-and-tenants">https://aspnetboilerplate.com/Pages/Doc ... nd-tenants</a>.

    Thanks.

  • User Avatar
    0
    yrojas created

    Thanks, problem solved.