我们在EFCore中使用Table Per Hierarchy设置了以下模型(为简洁起见而简化):
public class User {
public int Id { get; set; }
}
public class Student : User {
public Company Company { get; set; }
}
public class Teacher : User {
public Company School { get; set; }
}
我希望所有用户(包括学生和教师)都包含所有参考。基本上我想在一个查询中执行以下操作:
_context.Users.OfType<Teacher>().Include(x => x.School);
_context.Users.OfType<Student>().Include(x => x.Company);
有办法吗?
答案 0 :(得分:0)
根据Ivan的评论,当前(前EFCore 2.1)解决方案是:
var users = _context.Users.Where(_filter_conditions_here).Skip().Take(_if_you_like_);
var userIds = users.Select(x => x.Id).ToList()
_context.Users.OfType<Teacher>().Where(x => userIds.Contain(x.Id)).Include(x => x.School).ToList();
_context.Users.OfType<Student>().Where(x => userIds.Contain(x.Id)).Include(x => x.Company).ToList();