-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: How to read from App.confi and not from hibernate.cfg.xml
PostPosted: Thu May 15, 2008 3:52 pm 
Regular
Regular

Joined: Tue Oct 16, 2007 9:45 am
Posts: 93
Hibernate version: 1.2.1

I cannot get NH to read from app.config. How do you people do it? It is always looking for hibernate.cfg.xml file in bin directory. So I create cfg file and put it in the roor of the application and make it embeded resource. It is still looking for it in bin directory. So what is going on here?

App.Config file:


Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>

  <!-- This section is for NHibernate configuration-->
  <hibernate-configuration>
    <!-- an ISessionFactory instance -->
    <session-factory xmlns="urn:nhibernate-configuration-2.2">
      <property name="connection.provider">
        NHibernate.Connection.DriverConnectionProvider
      </property>
      <property name="dialect">
        NHibernate.Dialect.MsSql2005Dialect
        <!--NHibernate.Dialect.MySQL5Dialect-->
      </property>
      <property name="connection.driver_class">
        NHibernate.Driver.SqlClientDriver
        <!--NHibernate.Driver.MySqlDataDriver-->
      </property>
      <property name="connection.connection_string">
        Server=SQLEXPRESS;User Id=sa;Password=password;Database=AR200;
            </property>
      <property name="show_sql">
        true
      </property>
      <mapping assembly="Data" />
    </session-factory>
  </hibernate-configuration>
 
  <!-- This section contains the log4net configuration settings -->
  <log4net debug="true">
    <!-- Define some output appenders -->
    <appender name="trace" type="log4net.Appender.TraceAppender, log4net">
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
      </layout>
    </appender>

    <appender name="console" type="log4net.Appender.ConsoleAppender, log4net">
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
      </layout>
    </appender>

    <appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" >

      <param name="File" value="NHibernatelog.txt" />
      <param name="AppendToFile" value="true" />
      <param name="RollingStyle" value="Date" />
      <param name="DatePattern" value="yyyy.MM.dd" />
      <param name="StaticLogFileName" value="true" />

      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
      </layout>
    </appender>

    <!-- Setup the root category, add the appenders and set the default priority -->

    <root>
      <priority value="DEBUG" />
      <appender-ref ref="console" />
    </root>
  </log4net>
</configuration>


and I do this in test project

Code:
       [SetUp]
        public void Init()
        {

            Console.WriteLine("Init() start");

            //setup log4net logging
            log4net.Config.XmlConfigurator.Configure();

            //setup NHibernate
            if (_factory == null)
            {
                Configuration config = new Configuration();
               
                config.Configure();
                _factory = config.BuildSessionFactory();
            }

            _session = _factory.OpenSession();

            Console.WriteLine("Init() end");
        }


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 16, 2008 2:35 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Quote:
System-level properties can only be set manually by setting static properties of NHibernate.Cfg.Environment
class or be defined in the <nhibernate> section of the application configuration file. These properties cannot be
set using Configuration.SetProperties or be defined in the <hibernate-configuration> section of the application
configuration file.


"dialect" is one of the options that can't be set using "hibernate-configuration" style. According to the documentation, "dialect" should be optional or have a default, but I always got an MappingException claiming, "dialect" was not set.

Use the "nhibernate" style with the System.Configuration.NameValueSectionHandler instead. This should be working. And check the build action for App.config. Shouldn't be "embedded resource".

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 16, 2008 9:35 am 
Regular
Regular

Joined: Tue Oct 16, 2007 9:45 am
Posts: 93
I got it somehow to work after re-openning the solution, but thanks for the response.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.