NHibernate连接问题

时间:2010-09-15 16:08:17

标签: asp.net-mvc nhibernate

我正在使用sql server 2005

但是有错误消息         _sessionFactory = configuration.BuildSessionFactory();

有什么问题,我该如何纠正?


我的Hibernate.cfg.xml

 <property name="connection.driver_class">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
<!--<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>-->
<property name="connection.connection_string">Server=localhost\SQLServer2005;database=NHibernate101;Integrated Security=True;</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<mapping assembly="Infrastructure"></mapping>

和我的web.config

    <connectionStrings>
    <add name="ApplicationServices" connectionString="Server=localhost\SQLServer2005;database=NHibernate101;" providerName="NHibernate.Connection.DriverConnectionProvider"/>
</connectionStrings>

错误信息是:

  

NHibernate.HibernateException未被用户代码处理     Message =无法从NHibernate.Connection.DriverConnectionProvider创建驱动程序。     来源= NHibernate的     堆栈跟踪:          在NHibernate.Connection.ConnectionProvider.ConfigureDriver(IDictionary 2 settings) at NHibernate.Connection.ConnectionProvider.Configure(IDictionary 2设置)          在NHibernate.Connection.ConnectionProviderFactory.NewConnectionProvider(IDictionary 2 settings) at NHibernate.Cfg.SettingsFactory.BuildSettings(IDictionary 2个属性)          在NHibernate.Cfg.Configuration.BuildSettings()          在NHibernate.Cfg.Configuration.BuildSessionFactory()          在Core:Domain.Repositories.NHibernateHelper.get_SessionFactory()在C:\ Documents and Settings \ nHibernate101 \ NHibernate101 Final \ NHibernate101 \ Core \ Domain \ Repositories \ NHibernateHelper.cs:第22行          在Core:Domain.Repositories.NHibernateHelper.OpenSession()在C:\ Documents and Settings \ nHibernate101 \ NHibernate101 Final \ NHibernate101 \ Core \ Domain \ Repositories \ NHibernateHelper.cs:第30行          在Core:Domain.Repositories.PostRepository.Core.IRepository.GetAll()在C:\ Documents and Settings \ nHibernate101 \ NHibernate101 Final \ NHibernate101 \ Core \ Domain \ Repositories \ PostRepository.cs:第59行          at NHibernate101.Controllers.PostsController.Index()in C:\ Documents and Settings \ nHibernate101 \ NHibernate101 Final \ NHibernate101 \ NHibernate101 \ Controllers \ PostsController.cs:第22行          在lambda_method(Closure,ControllerBase,Object [])          在System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase控制器,Object []参数)          在System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary 2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary 2个参数)          在System.Web.Mvc.ControllerActionInvoker。&lt;&gt; c__DisplayClassd.b__a()          在System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter过滤器,ActionExecutingContext preContext,Func 1 continuation) InnerException: System.InvalidCastException Message=Unable to cast object of type 'NHibernate.Connection.DriverConnectionProvider' to type 'NHibernate.Driver.IDriver'. Source=NHibernate StackTrace: at NHibernate.Connection.ConnectionProvider.ConfigureDriver(IDictionary 2设置)          InnerException:

3 个答案:

答案 0 :(得分:1)

您的配置不正确。使用这个:

<property name="hibernate.connection.provider">NHibernate.Connection.DriverConnectionProvider</property> 
<property name="hibernate.connection.driver_class">NHibernate.Driver.SqlServerCeDriver</property> 
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property> 
<property name="connection.connection_string">Server=localhost\SQLServer2005;database=NHibernate101;Integrated Security=True;</property> 
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property> 
<mapping assembly="Infrastructure"></mapping> 

答案 1 :(得分:1)

您收到该错误,因为在您的Hibernate.cfg.xml中,您告诉NHibernate将NHibernate.Connection.DriverConnectionProvider类用于connection.driver_class属性。 connection.driver_class属性需要一个IDriver接口,而NHibernate.Connection.DriverConnectionProvider则没有。

答案 2 :(得分:0)

你是否指定了SQL2000方言而不是2005年方言?我从来没有发生这种情况所以我不确定它是否会引发这个错误。