我正在尝试从列表中加载对象,但是我无法正确指定导航属性。
我已经设法使其与Include和ThenInclude一起使用。 (下面的代码)
我有这样的东西:
public class SubjectStudent
{
[Key]
public int Id { get; set; }
public Subject Subject { get; set; }
[Key]
public string Ra { get; set; }
public Student Student { get; set; }
}
public class Student
{
other properties...
public IList<SubjectStudent> SubjectStudent { get; set; }
}
作品:
var something = _context.Set<Student>().Include(sub => sub.SubjectStudent).ThenInclude(s => s.Subject).SingleOrDefault(s => s.Ra == "1");
Irepository:
T Get(Func<T, bool> where, params Expression<Func<T, object>>[] navigationProperties);
服务:
public Student GetByRa(string id)
{
return _iStudentRepository.Get(s => s.Ra == id, x => x.SubjectStudent, <something here to replace theninclude to access subject>);
}
我尝试过类似的事情:
return _iStudentRepository.Get(s => s.Ra == id, x => x.SubjectStudent, x => x.SubjectStudent.Select(xx => xx.Subject));
但这不起作用。
在表达式中仅包含SubjectStudent会返回空主题。
预期: 包含一个SubjectStudent列表的Student对象,它们都必须返回Student,并填充Subject对象。
谢谢!
答案 0 :(得分:0)
要使其正常工作,您必须将Subject
属性设为虚拟,并启用延迟加载。