0

作为 NHibernate (2.1.0) 的初学者,我正在尝试使用著名的 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 输出窗口中(请参阅本文底部的配置文件),但没有任何显示。

在这个阶段我只能猜测我的配置不正确,但我不知道如何修复它。


  • 应用程序配置

    <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>
    
  • 客户.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; }
        }
    }
    
  • 客户.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> 
    
4

3 回答 3

0

我解决此问题的第一步是询问您的调用正在生成什么 sql,然后尝试针对目标数据库运行它并查看您得到的结果

您可能需要将附加程序更改为控制台附加程序而不是跟踪。

于 2009-09-23T08:13:52.380 回答
0

您需要在配置对象上调用 configure 的两件事,如下所示:

   
Configuration configuration = new Configuration().Configure();

这期望在同一目录中有一个名为 Hibernate.cfg.xml 的文件。如果您有其他名称,则可以调用另一种方法来指定配置文件的名称。

其次你需要告诉NHibernate在哪里加载映射文件,在创建工厂之前是哪个程序集


configuration.AddAssembly(typeof(Customer).Assembly); //if in same assembly with  customer class

最后,您的客户类的所有成员都应该是虚拟的,否则您不能使用延迟加载。

于 2009-09-23T08:19:12.583 回答
0

搞定了 !

碰巧我想巧妙地发挥它,并将 XML 映射文件作为 CS 文件的依赖项。在这种情况下,嵌入资源似乎带有包含在 CS 文件中的第一个类的名称,而不是 XML 文件的名称。一张应该值一千字的图片: 解决方案和组装

所以我写了这个来让它工作:

ISessionFactory sessionFactory=new Configuration()
    .Configure()
    .AddResource(typeof(Customer).FullName, typeof(Customer).Assembly)
    .BuildSessionFactory();

让它发挥作用的经验教训:

  • 当您尝试使用没有已知映射的类时,NHibernate 不会以任何方式抱怨。我不确定我会称其为功能...
  • 在与您自己的会话打开之前IDbConnection,请打开连接本身。
  • 尝试玩聪明时,您学到的东西超出了您的预期;-)
于 2009-09-23T13:06:19.523 回答