我试图在实体上加载导航属性,但是在导航属性丢失或为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
}
我认为您需要在条件中指定导航属性:
| UserId | RoleId | GroupId |
| 1 | NULL | 1 |