NHibernate单元测试异常(FileNotFound hibernate.cfg.xml)

时间:2016-12-06 16:31:27

标签: c# nhibernate

我在web.config文件中配置了NHibernate,使用hibernate.cfg.xml配置了

我的web.config文件的NHibernate部分:

<configSections>
  <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>

<connectionStrings>
  <add name="DatabaseConnectionString" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='C:\path-to-my-project\MyProject\App_Data\Development-DB.mdf';Integrated Security=True" />
</connectionStrings>

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">
      NHibernate.Connection.DriverConnectionProvider
    </property>
    <property name="connection.driver_class">
      NHibernate.Driver.SqlClientDriver
    </property>
    <property name="connection.connection_string_name">
      DatabaseConnectionString
    </property>
    <property name="dialect">
      NHibernate.Dialect.MsSql2012Dialect
    </property>
    <property name="show_sql">
      true
    </property>
    <property name="current_session_context_class">
      web
    </property>
  </session-factory>
</hibernate-configuration>

我的NHibernate助手类:

public class NHibernateHelper
{
    private ISessionFactory _sessionFactory = null;

    /// <summary>
    /// In case there is an already instantiated NHibernate ISessionFactory,
    /// retrive it, otherwise instantiate it.
    /// </summary>
    public ISessionFactory SessionFactory
    {
        get
        {
            if (_sessionFactory == null)
            {
                var configuration = _getConfiguration();

                // bild a Session Factory
                _sessionFactory = configuration.BuildSessionFactory();
            }
            return _sessionFactory;
        }
    }

    private Configuration _getConfiguration()
    {
        Configuration configuration = new Configuration();
        configuration.Configure();

        configuration.AddMapping(_getMappings(Assembly.Load("MyProject")));
        return configuration;
    }

    private HbmMapping _getMappings(Assembly assembly)
    {
        var mapper = new ModelMapper();
        mapper.AddMappings(assembly.GetExportedTypes());

        var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
        return mapping;
    }

    public ISession OpenSession()
    {
        return SessionFactory.OpenSession();
    }

    public void CreateSession()
    {
        CurrentSessionContext.Bind(OpenSession());
    }

    public void CloseSession()
    {
        if (CurrentSessionContext.HasBind(SessionFactory))
        {
            CurrentSessionContext.Unbind(SessionFactory).Dispose();
        }
    }

    public ISession GetCurrentSession()
    {
        if (!CurrentSessionContext.HasBind(SessionFactory))
        {
            CurrentSessionContext.Bind(SessionFactory.OpenSession());
        }
        return SessionFactory.GetCurrentSession();
    }

    public void UpdateDatabaseSchema()
    {
        Configuration configuration = _getConfiguration();

        SchemaExport schema = new SchemaExport(configuration);
        schema.Execute(false, true, false);
    }
}

到目前为止一切顺利。一切正常。

现在问题。我有另一个单元测试项目,让我们称之为MyProject_Tests。在MyProject_Tests {I}引用MyProject

当我尝试在单元测试中从SessionFactory获取NHibernateHelper时,出现以下错误:

  

NHibernate.Cfg.HibernateConfigException:期间发生异常   持久层的配置。 ----&GT;   System.IO.FileNotFoundException:找不到文件   &#39; C:\路径到我的项目\测试\单元测试\ MyProject_Tests \ BIN \调试\ hibernate.cfg.xml的&#39;

我认为发生此错误是因为NHibernate无法在MyProject_Tests\web.config内找到配置。由于单元测试项目中没有web.config文件。

如何告诉NHibernate始终在web.config内查找MyProject文件,在MyProject_Tests内查找

我正在寻找相应的:

configuration.Configure("MyProject/path/to/hibernate.cfg.xml");

您可以在其中定义绝对路径,但可以与web.config一起使用。

我试图谷歌几个小时没有成功。 任何帮助表示赞赏。感谢。

0 个答案:

没有答案