代码之家  ›  专栏  ›  技术社区  ›  gvdm

仅在本地开发环境中获取数据的存储库

  •  0
  • gvdm  · 技术社区  · 6 年前

    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();
                [...]
            }
        }
    }
    

    在开发过程中,一切正常工作,并获取链接的数据。 现在我已经在I is 7服务器上部署了它,但它不再工作了。调试,我可以看到 defaultUsers 我在两个环境中使用相同的用户和相同的数据库。

    这是 UserTeam 实体。只是样板间的多对多映射 User Team 我的实体。

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

    你觉得这个怎么样?

    更新1:

    我直接执行SQL查询只是为了检查它是否可能是样板文件的问题或其他什么问题。这是我运行的代码(使用相同的方法)

    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
        }
    }
    

    好吧,数据是被获取的,我没有用户数据的空值。似乎ASPNET样板文件、实体框架核心和SQL引擎之间出现了一些问题。你有什么线索吗?

    更新2: 我在考虑一个关于 用户 实体,所以我检查了这个:

    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();
                [...]
            }
        }
    }
    

    在这里,我在 用户 实体来读取所有用户。嗯,在开发机器上,我可以获取所有用户,在部署机器上,只有 admin 返回用户。在这两种情况下,我都使用 管理员 用户。 我认为 用户团队 存储库未获取非空用户,因为出于某种原因 用户 你怎么认为?

    2 回复  |  直到 6 年前
        1
  •  0
  •   Alper Ebicoglu    6 年前

    问题是您试图以错误的方式访问DbContext。另外,不应该从DbContext检索用户列表。您必须使用已经注入的存储库。

     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>>();
                //...
            }
    

    mapping how to use repositories

        2
  •  0
  •   gvdm    6 年前

    我发现了问题。在生产环境中,我使用 admin 用户,但没有选择任何租户,因此我登录到 .

    当前租户:未选择

    在登录页面上。 使用 Default 租户(或任何工作租户)解决了问题