Repository fetching data only in some cases

asp.net-core aspnetboilerplate c# entity-framework-core

Question

I have a ASPNET Boilerplate project; the piece of code below is executed both from a service and from a background job

    private readonly IRepository<UserTeam> _userTeamsRepository;
    [...]
    public List<UserTeam> GetUsers(){
        var defaultUsers = _userTeamsRepository
            .GetAllIncluding(ut => ut.Team, ut => ut.User)
            .Where(ut => ut.Team.AlwaysIncluded)
            .ToList();
    }

It fetches the data from a join table UserTeam between the Users table and a Teams table (one User belongs to multiple Teams, in one Team there can be multiple Users).

As I said, the GetUsers() method is referenced both in a web service and in a background job. Using a breakpoint I can see that when the method is called in the web service the returned list is full of UserTeam entities containing both the linked Team and User entities (which is what I want).

On the other hand, when it is called from the background job only the Team field is populated, while theUser part is null.

It is a very strange problem because the piece of code is the same and the only different thing is the fact that it is called in different contexts of the same application.

Do you know anything that may be causing this issue?

Thank you

1
0
7/24/2018 8:12:41 AM

Accepted Answer

As per the Tseng comment, the background job is cross tenant so it does not have access to the User table information.

I fixed this issue by fetching the User data before enqueuing the background job and passing the data to the input object of the job.

0
7/24/2018 9:47:50 AM

Popular Answer

You can explicitly set the current tenant with AbpSession

public class MyService
{
    private readonly IAbpSession _session;

    public MyService(IAbpSession session)
    {
        _session = session;
    }

    public void Test()
    {
        using (_session.Use(42, null))
        {
          var defaultUsers = _userTeamsRepository
            .GetAllIncluding(ut => ut.Team, ut => ut.User)
            .Where(ut => ut.Team.AlwaysIncluded)
            .ToList();
        }
    }
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow