Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Joe_m

Nhibernate

Recommended Posts

estou inciando com o nhibernate e estou empacado em um erro se alguem puder me ajudar

namespace NHTOOLS
{
    public class NHibernateHelper
    {

        private const string CurrentSessionKey = "nhibernate";

        private static NHibernate.Cfg.Configuration config;

        private static readonly ISessionFactory sessionFactory;

        static NHibernateHelper()
        {
           
                config = new NHibernate.Cfg.Configuration();
                
                sessionFactory = config.BuildSessionFactory();--> aqui nesta linha da o erro

                sessionFactory = config.Configure().BuildSessionFactory();
          
        }

        public static ISession GetCurrentSession()
        {

            CloseSession();

            HttpContext context = HttpContext.Current;

            ISession currentSession = context.Items[CurrentSessionKey] as ISession;

            if (currentSession == null)
            {

                currentSession = sessionFactory.OpenSession();

                context.Items[CurrentSessionKey] = currentSession;

            }

            return currentSession;

        }

        public static void CloseSession()
        {

            HttpContext context = HttpContext.Current;

            ISession currentSession = context.Items[CurrentSessionKey] as ISession;

            if (currentSession == null)
            {

                // No current session

                return;

            }

            currentSession.Close();

            context.Items.Remove(CurrentSessionKey);

        }

        public static void CloseSessionFactory()
        {

            if (sessionFactory != null)
            {

                sessionFactory.Close();

            }

        }
    }
}

APP.Config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<configSections>
		<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
		<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
	</configSections>

	<nhibernate>
		<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
		<add key="hibernate.dialect" value="NHibernate.Dialect.FirebirdDialect"/>
		<add key="hibernate.connection.driver_class" value="NHibernate.Driver.FirebirdClientDriver"/>
		<add key="hibernate.connection.connection_string" value="User=SYSDBA;Password=masterkey;Database=D:\vs2008\Agenda\SLAGENDA\BD\AG.FDB;Dialect=3;ServerType=1;"
        />
	</nhibernate>

	<log4net debug="false">

		<!-- Define some output appenders -->
		<appender name="trace"
              type="log4net.Appender.TraceAppender, log4net">
			<layout type="log4net.Layout.PatternLayout,log4net">
				<param name="ConversionPattern"
                     value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n" />
			</layout>
		</appender>

		<appender name="console"
              type="log4net.Appender.ConsoleAppender, log4net">
			<layout type="log4net.Layout.PatternLayout,log4net">
				<param name="ConversionPattern"
                     value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n" />
			</layout>
		</appender>

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

			<param name="File" value="h:\log.txt" />
			<param name="AppendToFile" value="false" />
			<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>

		<logger name="NHibernate">
			<level value="DEBUG" />
		</logger>


	</log4net>


	<runtime>
		<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
			<qualifyAssembly partialName="FirebirdSql.Data.FirebirdClient"
                fullName="FirebirdSql.Data.FirebirdClient, Version=2.5.2.0, Culture=neutral, PublicKeyToken=3750abcc3150b00c" />
		</assemblyBinding>
	</runtime>
</configuration>
nhibernate.cfg.xml

<?xml version="1.0" encoding="utf-8" ?>
<!-- To work with FireBird you must add the assembly binding for FirebirdClient ADO.NET 2.0 DataProvider
to your App.config.
Example:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
	<qualifyAssembly partialName="FirebirdSql.Data.FirebirdClient" fullName="FirebirdSql.Data.FirebirdClient, Version=2.0.1.0, Culture=neutral, PublicKeyToken=3750abcc3150b00c" />
</assemblyBinding>
</runtime>

  --><!--  
This template was written to work with NHibernate.Test.
Copy the template to your NHibernate.Test project folder and rename it in hibernate.cfg.xml and change it 
for your own use before compile tests in VisualStudio.

  -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
	 <session-factory name="nhibernate">
		<property name="connection.driver_class">NHibernate.Driver.FirebirdClientDriver</property>
		<property name="connection.connection_string">Server=localhost; Database=D:\vs2008\Agenda\SLAGENDA\BD\AG.fdb; User=SYSDBA;Password=masterkey</property>
		<property name="show_sql">false</property>
		<property name="dialect">NHibernate.Dialect.FirebirdDialect</property>
		<property name="command_timeout">60</property>
		<property name="query.substitutions">true 1, false 0, yes 1, no 0</property>
		<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
	</session-factory>
</hibernate-configuration>

Compartilhar este post


Link para o post
Compartilhar em outros sites

Bom, você deve enviar tambem o erro que ocorreu.

Quando você chama o método BuildSessionFactory, o Nhibernate vai ler todas os seus mapeamentos e classes e vai montar a estrategia dele.

Então o erro pode ser qualquer um, desde um erro no mapeamento, como esquecer de colocar um membro de classe como virtual.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Segue abaixo o erro

 

TypeInitializationException estava sem tratamento
O inicializador de tipo de 'NHTOOLS.NHibernateHelper' acionou uma exceção

Segue abaixo o erro

 

TypeInitializationException estava sem tratamento
O inicializador de tipo de 'NHTOOLS.NHibernateHelper' acionou uma exceção
Bom Dia !!!

 

E ai Danimar esse erro ajudou ou não?

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.