0

我正在使用 NHibernate 访问 SQL Server 数据库。我希望能够加密连接字符串。阅读 StackOverflow 和其他地方,我找到了将连接字符串分离到配置文件中的 connectionStrings 部分并在 nHibernate 的 connection_string_name 字段中按名称引用连接字符串的说明。

到目前为止,我已遵循所有说明。如果我只配置了 connection_string 字段,我的查询都可以成功。如果我将它分开,我会在尝试运行查询时遇到异常。

.config 文件包含这些部分。

<configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate">
    </section>
</configSections>

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
        <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
        <property name="connection.connection_string_name">nHibernateConnection</property>
        <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
    </session-factory>
</hibernate-configuration>

<connectionStrings>
    <add name="nHibernateConnection" connectionString="Data Source=(local);Password=Rapunz3l!;Persist Security Info=True;User ID=sa;Initial Catalog=VHT_Config" />
</connectionStrings>

初始化代码执行此操作。

var config = new Configuration();
config.Configure();
sessionFactory = config.BuildSessionFactory();

然后查询代码执行此操作:

using (ISession session = sessionFactory.OpenSession())
{
    return session.CreateQuery("from myTable").List();
}

我得到以下异常。

myTable is not mapped [from WSConfiguration]
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.CreateFromElement(String path, IASTNode pathNode, IASTNode alias, IASTNode propertyFetch)
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)
at VHT.Data.VHDAO.RetrieveAll(String tableClass) in C:\Projects\VH\Infrastructure\VHT.Data\VHDAO.cs:line 99
at MyWS.MyTableComponent.GetMyTableDataSet() 
in MyWS\MyDBComponent.cs:line 169

知道为什么只有在配置 connection_string_name 时才会失败?我错过了什么吗?

谢谢!

4

1 回答 1

0

CreateQuery 将 HQL 字符串作为参数。使用 HQL 时,您是从对象而不是表中查询。所以,

session.CreateQuery("from myTable").List();

仅当您的班级名称为myTable 时才有效,但是如果您的班级名称为MyTable,您最终将收到错误

myTable is not mapped 

因此,请尝试将myTable更改为MyTable以符合您的类命名约定。

于 2013-07-28T05:33:23.627 回答