作爲NHibernate(2.1.0)的初學者,我試圖用着名的Northwind數據庫設置我的第一個單元測試。該測試是這樣的(配置文件可以在這個問題的末尾找到):用NHibernate映射Northwind客戶的問題
ISessionFactory sessionFactory=new Configuration().BuildSessionFactory();
ISession session=sessionFactory.OpenSession();
IList<Customer> list=session.CreateCriteria<Customer>().List<Customer>();
Assert.AreEqual(91, list.Count);
的問題是,list.Count
始終爲0
- 我曾嘗試打開會話提供它我自己
IDbConnection
,在Sql Server 2008,MS Access和SQLite(我肯定100%確定數據庫設置和連接是有效的)無濟於事。 - 我試圖讓生成的SQL顯示在VS2008輸出窗口上(請參閱本文後面的配置文件),但沒有任何顯示。
我只能猜測在這個階段我的配置是不正確的,但我不知道如何解決它。
的App.config:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="dialect">NHibernate.Dialect.SQLiteDialect</property> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property> <property name="connection.connection_string">Data Source=S:\Work\SVN\src\Northwind\SQL\Northwind.db</property> <property name='proxyfactory.factory_class'>NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property> <property name="show_sql">true</property> </session-factory> </hibernate-configuration> <log4net> <appender name="DebugSQL" type="log4net.Appender.TraceAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <root> <level value="DEBUG" /> <appender-ref ref="DebugSQL" /> </root> </log4net>
Customer.cs:
namespace Northwind.DomainModel.NHibernate { public class Customer { public string CustomerID { get; set; } public string CompanyName { get; set; } public string ContactName { get; set; } public string ContactTitle { get; set; } public string Address { get; set; } public string City { get; set; } public string Region { get; set; } public string PostalCode { get; set; } public string Country { get; set; } public string Phone { get; set; } public string Fax { get; set; } } }
Customer.hbm.xml:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Northwind.DomainModel" namespace="Northwind.DomainModel.NHibernate" > <class name="Customer" table="Customers"> <id name="CustomerID" column="CustomerID" type="String" length="5"> <generator class="assigned" /> </id> <property name="CompanyName" /> <property name="ContactName" /> <property name="ContactTitle" /> <property name="Address" /> <property name="City" /> <property name="Region" /> <property name="PostalCode" /> <property name="Country" /> <property name="Phone" /> <property name="Fax" /> </class> </hibernate-mapping>
客戶ID時的長度是5,而不是20。這是不太可能但解決您的問題。 – 2009-09-23 08:29:45
感謝您指出:從我發現的一些博客愚蠢的複製和粘貼。但這意味着你閱讀整件事情! – Mac 2009-09-23 12:09:09