具有私有支持字段的 nhibernate 集合查询
我在学生和教授 (_students_selected) 之间的数据库(带有桥接表)中建模了一对多关系,在我的实体中,我将其建模为一对多关系,即教授有一个学生。
HasManyToMany<Student>(Reveal.Member<Professor>("_students"))
.Table("_students_selected").ChildKeyColumn("student_key").ParentKeyColumn("professor_key");
public class Professor
{
private IList<Students> _students;
public virtual Student Student
{
get { return _students.FirstOrDefault(); }
}
}
上面的方法在获取数据时有效,但是在查询教授时,我无法添加学生的 where 条件,因为实际数据映射到私人支持字段 _students。我该如何查询这个?下面的代码不起作用。
_unitOfWork.Session.QueryOver<Professor>().Where(i => i.Student.Id == 24).List();
I have a mant-to-many relationship modeled in the database (with a bridge table) between Student and Professor (_students_selected) , in my entites i have modeled it as a one-to-many relationship i.e. a Professor has one Student.
HasManyToMany<Student>(Reveal.Member<Professor>("_students"))
.Table("_students_selected").ChildKeyColumn("student_key").ParentKeyColumn("professor_key");
public class Professor
{
private IList<Students> _students;
public virtual Student Student
{
get { return _students.FirstOrDefault(); }
}
}
The above works when getting the data however when querying over the Professors i am unable to add a where condition on the students because the actual data is mapped to the private backing field _students. How do i query this? code below does not work.
_unitOfWork.Session.QueryOver<Professor>().Where(i => i.Student.Id == 24).List();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NHibernate 无法将属性内的 C# 代码转换为 SQL,它只能使用映射的属性。要么在语句中使用集合(当然需要是公共/内部的),要么过滤内存中的结果(但要小心 select n + 1 问题)。
NHibernate can't translate your C# code inside the property to SQL, it can only work with mapped properties. Either use the collection in the statement (which needs to be public/internal then of course) or filter the results in memory (but be careful with select n + 1 problems then).