NHibernateHelper-许多表

NHibernateHelper-许多表,nhibernate,Nhibernate,我是NHibernate的新手,我根据教程:。因此,我有NHibernateHelper: public class NHibernateHelper { private static ISessionFactory _sessionFactory; private static ISessionFactory SessionFactory { get { if (_sessionFactory == null) { var configuration =

我是NHibernate的新手,我根据教程:。因此,我有NHibernateHelper:

public class NHibernateHelper {
private static ISessionFactory _sessionFactory;

private static ISessionFactory SessionFactory
{
  get
  {
    if (_sessionFactory == null)
    {
      var configuration = new Configuration();

      configuration.Configure(); 

      configuration.AddAssembly(typeof (Product).Assembly);

      _sessionFactory = configuration.BuildSessionFactory();
    }

    return _sessionFactory;
  }
}

public static ISession OpenSession()
{
  return SessionFactory.OpenSession();
}  }
但我也有实体类别和用户?我是否需要使用code AddAssembly将每个实体添加到配置中??因为当我添加代码时:

configuration.AddAssembly(typeof (Product).Assembly);
configuration.AddAssembly(typeof(Category).Assembly);
我有一个错误:


无法编译映射文档:mvcapapplication1.Mappings.Product.hbm.xml

首先检查是否已将所有映射文件(*.hbm.xml)的“生成操作”设置为“嵌入资源”。这是非常重要的

然后只需添加一次对
AddAssembly
的调用,因为NHibernate足够聪明,可以扫描程序集,嗅出所有映射到所有嵌入
hbm.xml
文件的
实体

例如您只需提供一次包含所有实体的程序集
:-

_配置.AddAssembly(产品的类型).Assembly)


NHibernate现在将自动查找
类别
(以及所有其他类别),只要它们与
产品
位于同一程序集中。HTH

您也可以将映射标记添加到web.config中,而不是在SessionFactory初始化的代码中添加它。然后,您的代码将如下所示:

if (_sessionFactory == null)
{
  var configuration = new Configuration();

  configuration.Configure(); 

  _sessionFactory = configuration.BuildSessionFactory();
}
在web配置中,您必须指明所有映射所在的程序集,如下所示:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
  <property name="connection.provider">
    NHibernate.Connection.DriverConnectionProvider
  </property>
  <property name="dialect">
    NHibernate.Dialect.MsSql2005Dialect
  </property>
  <property name="connection.driver_class">
    NHibernate.Driver.SqlClientDriver
  </property>
  <property name="connection.connection_string">
    -- YOUR STRING CONNECTION --
  </property>
  <property name="proxyfactory.factory_class">
    NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle
  </property>
  <mapping assembly="You.Assembly.Namespace" />
</session-factory>

NHibernate.Connection.DriverConnectionProvider
NHibernate.dialogue.mssql2005dialogue
NHibernate.Driver.SqlClientDriver
--您的字符串连接--
NHibernate.ByteCode.Castle.proxyFactory,NHibernate.ByteCode.Castle


作为重要的配置标记“mapping assembly=“Your.assembly.Namespace”。正如前面提到的另一个贡献者,将每个hbm.xml文件标记为嵌入式资源非常重要,否则它将是您从未创建过的。通过这样做,您只需在此程序集(项目)内创建所有映射NH配置时将自动读取这些信息。

请使用您的
产品
类和相关映射更新您的问题?