开发者

nhibernate collection query with private backing field

开发者 https://www.devze.com 2023-04-04 21:45 出处:网络
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-开发者_JS百科many rela

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-开发者_JS百科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();


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号