我正在使用NHibernate來訪問SQL Server數據庫。我希望能夠加密連接字符串。閱讀StackOverflow和其他地方,我發現了指令將連接字符串分隔到配置文件中的connectionStrings部分,並在nHibernate的connection_string_name字段中按名稱引用連接字符串。無法讓NHibernate在分離連接字符串後執行查詢
到目前爲止,我已遵循所有說明。如果我只用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配置時纔會失敗?我錯過了什麼嗎?
謝謝!
這與連接字符串無關。 myTable沒有映射類。也許你想'session.CreateSqlQuery(「select * from myTable」)。List();'這是一個Sql,沒有HQL查詢 – Firo
這很有趣。我會嘗試。奇怪的是,它會給其他connection_string配置沒有錯誤。 – MarkP
不幸的是,改變打破了一切。我知道這很奇怪,但連接字符串實際上是我改變的唯一的東西。爲什麼它需要一個而不是另一個的映射? – MarkP