Repository fetching data only in local development environment

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

Question

I have a ASPNET Boilerplate project which in one application service uses a repository to access some data in the DB

public class AnAppService : AsyncCrudAppService<MyEntity, MyEntityDto, int, PagedAndSortedResultRequestDto, MyEntityDto, MyEntityDto>, IAnAppService
{
    private readonly IRepository<UserTeam> _userTeamsRepository;

    public AnAppService(IRepository<MyEntity> repository,
            IRepository<UserTeam> userTeamsRepository)
           : base(repository)
        {
            _userTeamsRepository = userTeamsRepository;
        }

        public override Task<MyEntityDto> Create(MyEntityDto input)
        {
            [...]
            var myDbContext = _myDbContextProvider.GetDbContext();
            var uteams = // Here the items have null User objects, though it was included in the query
                myDbContext.UserTeams
                .Include(ut => ut.User)
                .Where(ut => ut.Team.Id == teamId)
                .ToList();
            [...]
        }
    }
}

During the development everything works well and the linked data is fetched. Now I've deployed it on a IIS 7 server but it is not working anymore. Debugging, I can see that the defaultUsers list is not empty (and the number of items equals the items in the development machine), but the single items are null. I'm using the same user with the same DB for both the environments.

This is the definition of the UserTeam entity. It's simply a many-to-many mapping between the Boilerplate's User entity and a Team entity of mine.

public class UserTeam : Entity<int>
{
    public User User { get; set; }
    public Team Team { get; set; }
}

What do you think about this?

UPDATE 1:

I executed directly the SQL query just to check if it could be a Boilerplate's problem or something else. This is the code I run (in the same method)

string queryString = "SELECT u.name name FROM UserTeams ut join AbpUsers u on ut.UserId = u.Id";
using (SqlConnection connection = new SqlConnection(connectionString))
{
    SqlCommand command = new SqlCommand(queryString, connection);
    connection.Open();
    SqlDataReader reader = command.ExecuteReader();
    while (reader.Read())
    {
        var name = reader["name"]; // The name variable has a value
    }
}

Well, the data is fetched and I have not null values for the user's data. It seems that something is breaking between ASPNET Boilerplate, Entity Framework Core and the SQL engine. Do you have any clue?

UPDATE 2: I was thinking about a permission problem on the read operation of the User entity, so I checked this:

public class AnAppService : AsyncCrudAppService<MyEntity, MyEntityDto, int, PagedAndSortedResultRequestDto, MyEntityDto, MyEntityDto>, IAnAppService
{
    private readonly IRepository<User, long> _usersRepository;

    public AnAppService(IRepository<MyEntity> repository,
        IRepository<UserTeam> userTeamRepository)
           : base(repository)
        {
            _userTeamRepository = userTeamRepository;
        }

        public override Task<MyEntityDto> Create(MyEntityDto input)
        {
            [...]
            var users = _usersRepository.GetAll();
            [...]
        }
    }
}

Here I use a simple repository on the User entity to read all the users. Well, while on the development machine I can fetch all the users, on the deploy machine only the admin user is returned. In both cases I'm logged in with theadmin user. I think that the UserTeam repository is not fetching not-null users because for some reason the User table is not accessible to the logged user (but it is the same user for both the environment!) What do you think?

1
0
8/23/2018 12:39:27 PM

Accepted Answer

I found the issue. In the production environment I was logging in with the admin user, but not selecting any tenant, so I was logging into the . tenant, shown as

Current tenant: Not selected

on the login page. Logging in using the Default tenant (or any working tenant) fixed the problem

0
8/23/2018 1:02:34 PM

Popular Answer

the problem is that you are trying to access the DbContext in a wrong way. beside you shouldn't retrieve the list of users from DbContext. You have to use your repository which you already injected.

 public override Task<MyEntityDto> Create(MyEntityDto input)
        {
            //...
            var uteams = _userTeamsRepository.GetAllIncluding(x=>x.User).Where(ut => ut.Team.Id == teamId).ToList();
            //and you need to map uteams to your DTO list. 
            //var uteamsDto = uteams.MapTo<List<MyTeamDto>>();
            //...
        }

See mapping and how to use repositories .



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