I am using NHibernate to access a SQL Server database. I want to be able to encrypt the connection string. Reading StackOverflow and elsewhere, I found instructions to separate the connection string out to a connectionStrings section in the config file and reference the connection string by name in the nHibernate's connection_string_name field.
I have followed all of the instructions so far. If I have it configured with just the connection_string field, my queries all work successfully. If I separate it out, I get an exception when attempting to run a query.
The .config file contains these sections.
<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>
The initialization code does this.
var config = new Configuration();
config.Configure();
sessionFactory = config.BuildSessionFactory();
Then the query code does this:
using (ISession session = sessionFactory.OpenSession())
{
return session.CreateQuery("from myTable").List();
}
I get the following exception.
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
Any idea why this would fail only when the connection_string_name is configured? Am I missing something?
Thank you!
CreateQuery takes a HQL string as parameter. When using HQL you are querying from the object and not the table. Therefore,
will only work if the name of your class has a name myTable however if your class has a name MyTable, you will end up getting the error
So try changing the myTable to MyTable to conform with your class naming conventions.