我正在使用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时才会失败?我错过了什么吗?
谢谢!
答案 0 :(得分:0)
CreateQuery将HQL字符串作为参数。使用HQL时,您要查询对象而不是表。因此,
session.CreateQuery("from myTable").List();
只有在您的班级名称的名称为 myTable 时才有效,但如果您的班级名称为 MyTable ,您最终会收到错误
myTable is not mapped
因此,请尝试将 myTable 更改为 MyTable ,以符合您的类命名约定。