ripper234
ripper234

Reputation: 230038

"Table not mapped" with NHibernate + Active Record

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"

Upvotes: 2

Views: 3277

Answers (1)

Mauricio Scheffer
Mauricio Scheffer

Reputation: 99730

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");

Upvotes: 3

Related Questions