代码之家  ›  专栏  ›  技术社区  ›  Mac

使用NHibernate映射Northwind客户时出现的问题

  •  0
  • Mac  · 技术社区  · 16 年前

    作为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

    • 我试图以自己的方式打开会议 IDbConnection

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


    • 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>
      
    • :

      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; }
          }
      }
      
    • :

      <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> 
      
    3 回复  |  直到 16 年前
        1
  •  0
  •   Fen    16 年前

    我解决这个问题的第一步是询问您的调用正在生成什么sql,然后尝试对目标db运行它,看看会得到什么结果

        2
  •  0
  •   Emmanuel    16 年前

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

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

        3
  •  0
  •   Community Mohan Dere    8 年前

    搞定了!

    碰巧我想巧妙地使用它,并将XML映射文件作为CS文件的依赖项。在这种情况下,嵌入式资源似乎带有CS文件中包含的第一个类的名称,而不是XML文件的名称。一张图片据说值千言万语: Solution and Assembly

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

    • 当你试图使用一个没有已知映射的类时,NHibernate不会以任何方式抱怨。我不确定我会称之为一个功能。..
    • IDbConnection ,打开连接本身。