关于查询的NHibernate异常

Yoa*_*oav 1 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; }
}
Run Code Online (Sandbox Code Playgroud)

和映射类:

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

组态:

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

}
Run Code Online (Sandbox Code Playgroud)

并查询:IList项目;

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

我在查询行上遇到异常:

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)
Run Code Online (Sandbox Code Playgroud)

我认为我的查询有问题.

Vad*_*dim 5

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

.Mappings(m => m.FluentMappings.AddFromAssemblyOf<ProjectMap>())
Run Code Online (Sandbox Code Playgroud)