views:

491

answers:

1

I'm using Active Record with ActiveRecord Facility, and am trying to use a custom NHibernate query. Do I need to define a mapping for a class even though it extends ActiveRecordBase and has ActiveRecord attribute?

[ActiveRecord("VotesOnQuestions")]
public class VoteOnQuestion : ActiveRecordBase<VoteOnQuestion>
{
    [CompositeKey]
    public VoteKey Key { get; set; }

    [Property]
    public VoteType Vote { get; set; }
}

I'm trying to create the following query:

session.CreateQuery("SELECT vote, COUNT(*) FROM votesonquestions" +
                    " WHERE questionid = :questionId GROUP BY vote");

But I'm getting this exception:

"votesonquestions is not mapped"

+2  A: 

Just as the exception says, you need a class marked with [ActiveRecord] that maps the votesonquestions (I'm guessing it's called like that) table.

Inheriting from ActiveRecordBase is optional.

In the query you can only refer mapped classes (which are case-sensitive), not tables. So in this case, the query should be:

session.CreateQuery("SELECT vote, COUNT(*) FROM VoteOnQuestion" +
                    " WHERE questionid = :questionId GROUP BY vote");
Mauricio Scheffer