Search code examples
asp.net-mvcnhibernatecastle-activerecord

Castle ActiveRecord fails to detect DAO field in some queries


This query works fine:

DetachedCriteria filter = DetachedCriteria
                          .For(typeof(NotificationRecord), "nr")
                          .Add(Expression.Eq("nr.Submission.Id", 5));

return ActiveRecordMediator<NotificationRecord>.FindAll(filter);

This query fails with the exception message: could not resolve property: Submission.IsScheduledForNotification of: NotificationRecord

DetachedCriteria filter = DetachedCriteria
                          .For(typeof(NotificationRecord), "nr")
                          .Add(Expression.Eq("nr.Submission.IsScheduledForNotification", true));

return ActiveRecordMediator<NotificationRecord>.FindAll(filter);

To ensure ActiveRecord is recognizing IsScheduledForNotification, I do a simple query on the actual Submission object using the IsScheduledForNotification as filter like so and it works

ActiveRecordMediator<Submission>.Exists(Expression.Eq("IsScheduledForNotification", true));

Can someone say why this should happen?


Solution

  • Use CreateAlias() to indicate the joined entity, e.g.:

    DetachedCriteria
    .For(typeof(NotificationRecord), "nr")
    .CreateAlias("nr.Submission", "s")
    .Add(Expression.Eq("s.IsScheduledForNotification", true));
    

    You don't need this for the nr.Submission.Id query because there is no join there.