NHibernate错误集锦及配置技巧
?前一段時間在學習NHibernate的時候,被那些配置弄得糊里糊涂,有一些很細微的地方不知道錯在哪里,花了很多的時間去尋找,最后才調試通過,這里我就把遇到錯誤的地方以及怎么解決這些錯誤的方法貼出來,至于怎么利用NHibernate去開發,這個網絡上有很多的Demo,大家都可以從網絡上找得到并下載下來看,所以我就不附上了,OK...言規正傳,以下呢,主要就是在配置方面的三種錯誤:
?? <1>.你得注意你的*.hbm.xml配置檔中NHibernate的版本號.
?? <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
??????????此處的2.2代表了NHibernate的版本號,必須與你安裝的NHibernate的產品版本號相符.
??????????否則的話,舉個例子,若為urn:nhibernate-mapping-2.0,則會出現如下錯誤:
Could not find schema information for the element 'urn:nhibernate-mapping-2.0:hibernate-mapping'.
--------------------------------------------------------------------------------
<2>.在你的對照類中,必須注意屬性的類型,不能缺少virtual關鍵字.
???????? 屬性的類型必須是:public virtual式的.如:pubic virtual int ID
???????? 否則會出現如以下錯誤:
ex = {"The following types may not be used as proxies:\nTest.Model.Person: method get_ID should be virtual\nTest.Model.Person: method set_ID should be virtual"}
--------------------------------------------------------------------------------
<3>.必須注意App.config中的版本號問題.
???????? 應用程序配置檔中的
??????<section name="nhibernate"
????????????type="System.Configuration.NameValueSectionHandler,System,
Version=1.2.1.4000,Culture=neutral, PublicKeyToken=b77a5c561934e089"/>??
???? 其中的Version后面的值必須跟被引用的NHibernate組件的版本號是相同的.
????另外publickeyToken=b77a5c561934e089.
???? 否則會出現如下錯誤:
????An error occurred creating the configuration section handler for nhibernate: Could not load file or assembly 'System, Version=1.2.1.4000, Culture=neutral, PublicKeyToken=b77a5c566934e089' or one of its dependencies. 系統找不到指定的文件。 (E:\SampeCode\Console\Console01\bin\Debug\Console01.vshost.exe.config line 4)
--------------------------------------------------------------------------------
另附:對于NHibernate的ADO.NET屬性的配置可以有三種方式:
(1).利用App.config配置,如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
????<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.2.1.4000, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<nhibernate>
????<add key="hibernate.show_sql" value="true"/>
????<add key="hibernate.dialect" value="NHibernate.JetDriver.JetDialect,NHibernate.JetDriver"/>
????<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
????<add key="hibernate.connection.driver_class" value="NHibernate.JetDriver.JetDriver,NHibernate.JetDriver"/>
????<add key="hibernate.connection.connection_string" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\admin\桌面\NHibernateSample\NhibernateSampleA\missile.mdb"/>
????<add key="hibernate.query.substitutions" value="true=1;false=0"/>
</nhibernate>
</configuration>
按這種方法配置完后,在程序中就可以這樣使用它:
(1).
Configuration cfg=new Configuration().AddAssambly("NHibernateSampleA");
(2).
Configuration cfg=new Configuration();
cfg.AddClass(typeof(NHibernateSampleA.missile));
(2).利用在代碼中進行配置,如下:
Configuration cfg = new Configuration();
cfg.SetProperty("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
cfg.SetProperty("hibernate.connection.connection_string", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=missile.mdb");
cfg.SetProperty("hibernate.dialect", "NHibernate.JetDriver.JetDialect,NHibernate.JetDriver");
cfg.SetProperty("hibernate.connection.driver_class", "NHibernate.JetDriver.JetDriver,NHibernate.JetDriver");
cfg.SetProperty("hibernate.show_sql","true");
cfg.AddAssembly("NHibernateSample1");
(3).利用讀取配置檔的方式來進行,如下:
以下配置是寫在一個叫做MyAppconfig.cfg.xml的文件中.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="NhibernateSample1">
?? <!-- properties -->
<property name="connection.provider">
????NHibernate.Connection.DriverConnectionProvider
</property>
?? <property name="connection.driver_class">
???? NHibernate.JetDriver.JetDriver,NHibernate.JetDriver</property>
?? <property name="connection.connection_string">
???? Provider=Microsoft.Jet.OLEDB.4.0;Data Source=missile.mdb
</property>
?? <property name="show_sql">true</property>
?? <property name="dialect">
????NHibernate.JetDriver.JetDialect,NHibernate.JetDriver
</property>
<!-- mapping files -->
<mapping assembly="NhibernateSample1" />
</session-factory>
</hibernate-configuration>
寫完配置檔后,在程序里面加上配置檔的讀取.
Configuration cfg = new Configuration();
cfg.Configure(@"C:\SampleCode\NHibernate\MyApp.cfg.xml");
OK..這就是第三種的配置方法.
總結
以上是生活随笔為你收集整理的NHibernate错误集锦及配置技巧的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关系运算符号包括哪些(关系运算符)
- 下一篇: 石雪(说一说石雪的简介)