急于加载其键不为null的导航属性

问题描述 投票:0回答:1

我试图在实体上加载导航属性,但是在导航属性丢失或为NULL的情况下,我的查询返回NULL,我希望它只返回存储了实际值的导航属性,而不返回具有[ C0]。下面是到目前为止的内容摘要,基本上,我在对象“用户”中获得了导航属性的列表,然后包括了每个属性,但是有些记录的RoleId设置为Null,这使我的查询返回NULL

NULL

//表格示例

 public async Task<T> FindSingleAsync(Expression<Func<T, bool>> predicate)
        {
            var query = _table.AsQueryable()
                        .AsNoTracking()
                        .Where(predicate);

            foreach (var prop in Context.Model.FindEntityType(typeof(T)).GetNavigations().Where(x => x != null))
            {
                query = query.Include(prop.Name);
            }
            return await query.FirstOrDefaultAsync().ConfigureAwait(false);
        }

〜User〜

public class User{
      public int UserId {get; set; }
      public int? RoleId {get; set;}  //In database has NULL value
      public int? GroupId {get; set;} //In database has value of 1
}
c# repository-pattern ef-core-2.2 ef-core-3.0
1个回答
0
投票

我认为您需要在条件中指定导航属性:

|  UserId |  RoleId  |  GroupId  |
|   1     |  NULL  |    1    |
© www.soinside.com 2019 - 2024. All rights reserved.