前一段时间在学习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'.
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"}
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)
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>
<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>
<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");
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">
<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">
<property name="connection.driver_class">
NHibernate.JetDriver.JetDriver,NHibernate.JetDriver</property>
<property name="connection.connection_string">
<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">
<property name="show_sql">true</property>
<property name="dialect">
NHibernate.JetDriver.JetDialect,NHibernate.JetDriver
</property>
<!-- mapping files -->
<mapping assembly="NhibernateSample1" />
</session-factory>
</hibernate-configuration>
<mapping assembly="NhibernateSample1" />
</session-factory>
</hibernate-configuration>
写完配置档后,在程序里面加上配置档的读取.
Configuration cfg = new Configuration();
cfg.Configure(@"C:/SampleCode/NHibernate/MyApp.cfg.xml");
cfg.Configure(@"C:/SampleCode/NHibernate/MyApp.cfg.xml");
OK..这就是第三种的配置方法.