在Api中为登录用户检索数据的最佳方法是什么。我可以很好地检索所有用户的数据。
'ApplicationDbContext':
public DbSet<Category> Categories { get; set; }
'ApplicationUser':
public ICollection<Category> Categories { get; set; }
我的存储库:
public class CategoryRepository : ICategoryRepository
{
private ApplicationDbContext _ctx;
public CategoryRepository(ApplicationDbContext ctx)
{
_ctx = ctx;
}
//to do
public IEnumerable<Category> GetAll(string username)
{
return _ctx.Users.First(...).Categories.Include(x => x.Tasks);
}
public IEnumerable<Category> GetAllForFromAllUsers()
{
return _ctx.Categories.Include(x => x.Tasks);
}
}
如上所述,我不能在ICollection上使用扩展方法'Include'。 检索特定用户的数据的最佳方法是什么?
答案 0 :(得分:0)
你能试试吗?
var users = _ctx.Users
.Include(user => user.Categories)
.ThenInclude(cat => cat.Tasks)
.Where(u => u.username == "xx")
.ToList();