2010-03-09 38 views
2

我正在用asp.net中的MySql在Nhibernate中做一個項目。在這同時執行的代碼我想在下面一行持久層在配置持久層期間發生異常

的配置過程中

發生異常錯誤

ISessionFactory factory = new NHibernate.Cfg.Configuration().Configure).BuildSessionFactory(); 

因此,讓我幫麻煩拍攝錯誤。

這裏是我的配置文件

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> 
    <!-- an ISessionFactory instance --> 
    <session-factory> 
    <!-- properties --> 
    <property name="connection.provider"> 
     NHibernate.Connection.DriverConnectionProvider 
    </property> 
    <property name="connection.driver_class"> 
     NHibernate.Driver.MySqlDataDriver 
    </property> 
    <property name="connection.connection_string"> 
     Server=localhost;Database=hrms;User ID=test;Password=test; 
    </property> 
    <property name="dialect">NHibernate.Dialect.MySQL5Dialect</property> 
    <property name="show_mysql">true</property> 
    <!-- mapping files --> 
    <mapping resource="WebApp1.Job.hbm.xml" assembly="WebApp1" /> 
    </session-factory> 
</hibernate-configuration> 
+0

你也應該發佈你的配置。 –

+0

這個錯誤是非常通用的,可能是由很多因素造成的,但一個可能的原因可能是缺少或格式錯誤的「hibernate.cfg.xml」。 –

+0

我試過bt找不到 –

回答

2

不完整的配置吧?嘗試手動配置初始化,如下所示:

NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration(); 
cfg.SetProperty("dialect", "NHibernate.Dialect.MySQLDialect"); 
cfg.SetProperty("connection.driver_class", "NHibernate.Driver.MySqlDataDriver"); 
cfg.SetProperty("connection.connection_string", "Server=YourServer;Database=YourDatabase;User ID=YourId;Password=YourPass;CharSet=utf8"); 
cfg.SetProperty("proxyfactory.factory_class", "NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu"); 
cfg.AddAssembly("Your.Assembly.Name"); 
ISessionFactory sessionFactory = cfg.BuildSessionFactory(); 

如果一切正常,將其移動到XML,如果你喜歡。

+0

嗨嘉雅,對不起。其實我是這個NHibernate的新手。所以,讓我告訴你,我可以使用你的代碼來配置mannualy。 –

+0

在使用我們的代碼時,我得到了tat映射異常,由用戶代碼錯誤未處理 –

+0

您能否告訴我映射文件以及您將映射文件放在哪裏?也在上面的代碼中,假定映射文件位於Your.Assembly.Name程序集中。 –

0

我有一個類似的問題。問題是,我在Web.config文件中使用:

<section name="nhibernate" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> 
<nhibernate xmlns="urn:nhibernate-configuration-2.2"> 
    . 
    . 
    . 
</nhibernate> 

代替:

<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> 
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> 
    . 
    . 
    . 
</hibernate-configuration> 
1

請閱讀被拋出內部異常,它很可能你就會知道原因。根據我的經驗,它可以像代碼在bin/debug中查找hibernate.cfg.xml文件一樣簡單,並且無法找到它。

+0

這應該可能是一個評論,而不是一個答案。 – perigon

相關問題