关于查询的NHibernate异常

时间:2010-12-31 04:18:59

标签: nhibernate fluent-nhibernate

我正在获取执行最基本查询的映射异常。 这是我的域类:

public class Project
{
    public virtual string PK { get; set; }
    public virtual string Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
}

和映射类:

public class ProjectMap :ClassMap<Project>
{
    public ProjectMap()
    {
        Table("PROJECTS");
        Id(x => x.PK, "PK");
        Map(x => x.Id, "ID");
        Map(x => x.Name, "NAME");
        Map(x => x.Description, "DESCRIPTION");
    }
}

配置:

public ISessionFactory SessionFactory
{
    return Fluently.Configure()
        .Database(MsSqlCeConfiguration.Standard.ShowSql().ConnectionString(c => c.Is("data source=" + path)))
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Project>())
        .BuildSessionFactory();

}

并查询: IList项目;

using (ISession session = SessionFactory.OpenSession())
{
    IQuery query = session.CreateQuery("from Project");
    project = query.List<Project>();
}

我在查询行上收到了异常:

NHibernate.Hql.Ast.ANTLR.QuerySyntaxException: Project is not mapped [from Project]
   at NHibernate.Hql.Ast.ANTLR.SessionFactoryHelperExtensions.RequireClassPersister(String name)
   at NHibernate.Hql.Ast.ANTLR.Tree.FromElementFactory.AddFromElement()
   at NHibernate.Hql.Ast.ANTLR.Tree.FromClause.AddFromElement(String path, IASTNode alias)
   at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.fromElement()
   at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.fromElementList()
   at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.fromClause()
   at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.unionedQuery()
   at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.query()
   at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.selectStatement()
   at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.statement()
   at NHibernate.Hql.Ast.ANTLR.HqlSqlTranslator.Translate()
   at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.Analyze(HqlParseEngine parser, String collectionRole)
   at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.DoCompile(IDictionary`2 replacements, Boolean shallow, String collectionRole)
   at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.Compile(IDictionary`2 replacements, Boolean shallow)
   at NHibernate.Engine.Query.HQLQueryPlan..ctor(String hql, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
   at NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(String queryString, Boolean shallow, IDictionary`2 enabledFilters)
   at NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(String query, Boolean shallow)
   at NHibernate.Impl.AbstractSessionImpl.CreateQuery(String queryString)

我认为我的查询有问题。

2 个答案:

答案 0 :(得分:5)

Yoav问题是您需要指定映射文件所在的程序集,而不是您的实体所在的位置。将通话更改为

.Mappings(m => m.FluentMappings.AddFromAssemblyOf<ProjectMap>())

答案 1 :(得分:0)

首先确保您的映射类实际执行。您可以通过在ProjectMap类中放置断点然后进行调试以确保它被执行来实现。

using (ISession Session = SessionFactory.OpenSession())
{
    DetachedCriteria Filter = DetachedCriteria.For<Project>();
    var Projects = Filter.GetExecutableCriteria(Session).List<Project>();
}

尝试使用DetachedCriteria来确保您的查询不会错过拼写...我通常使用DetachedCriteria进行查询,因为那时我不需要知道Hql。