如何使用流畅的nhibernate(schemaexport)测试生成表?在asp.net上下文中

这是我的第一个流畅的hibernate项目。我在hibernate和nhibernate方面经验很少。 这个上下文对我来说是全新的,因为这是一个Web应用程序项目。 所以我有我的webapp项目,网上发现了大部分流利的nhibernate。 所以我有这个实体:
namespace myproject.model
{
  public class Request
  {
    public virtual string Id { get; private set; }
    public virtual Route route { get; set; }
    public virtual int code { get; set; }

  }
}

namespace myproject.model
{
  public class Route
  {
    public virtual string Id { get; private set; }
    public virtual string client_id { get; set; }
    public virtual IList<Request> requests { get; set; }

    public Route()
    {
        requests = new List<Request>();
    }

  }

}

//Mapping are like this.will only post one
namespace myproject.mappings
{
 public class RequestMap : ClassMap<Request>
 {
    public RequestMap()
    {
        Id(x => x.Id);
        Map(x => x.short_code);
        References(x => x.route);
    }
  }
}

//NhibernateSessionPerRequest
namespace myproject.Boostrap
{
  public class NhibernateSessionPerRequest : IHttpModule
  {
    private static readonly ISessionFactory _sessionFactory;

    static NhibernateSessionPerRequest()
    {
        _sessionFactory = CreateSessionFactory();
    }

    //all others IHttpModule event and methods are here
    private static ISessionFactory CreateSessionFactory()
    {

        FluentConfiguration configuration = Fluently.Configure().Database(MsSqlConfiguration.MsSql2005.
                                                                              ConnectionString(x => x.FromConnectionStringWithKey("localdb")))
            .Mappings(m => {
                            m.FluentMappings.AddFromAssemblyOf<myproject.model.Request>();
                            m.FluentMappings.AddFromAssemblyOf<myproject.model.Route>();
                           }
                     ).ExposeConfiguration((c)=> savedConfig = c);;

        return configuration.BuildSessionFactory();
    }

  }

   private static Configuration savedConfig;

    public static void BuildSchema(NHibernate.Cfg.Configuration config)
    {
        new SchemaExport(config).Create(false, true);
    }

    public static void BuildSchema(ISession session)
    {
        var export = new SchemaExport(savedConfig);
        export.Execute(false,true,false,session.Connection,null);
    }


}
我在webconfig中添加了模块
  <add name="NhibernateSessionPerRequest" type="myproject.Boostrap.NhibernateSessionPerRequest"/>
为了测试表的生成,我添加了一个测试项目(类库),将ref添加到nunit.framework 2.8.5和myproject。
namespace myproject.Tests
{
  [TestFixture]
  public class CanGenerateSchemaTestSuite
  {
    [Test]
    public void CanGenarateSchema()
    {
       NhibernateSessionPerRequest.BuildSchema(NhibernateSessionPerRequest.GetCurrentSession());

     }
  }
}
测试方法总是失败,我有这个例外:   CanGenerateSchemaTestSuite(1次测试),1次测试失败:子测试失败         CanGenarateSchema,失败:System.TypeInitializationException 如何在asp.net上下文中进行测试? 谢谢你读这篇文章。谢谢     
已邀请:
只是对其他解决方案的模糊评论;你不需要完全删除数据库文件;只是放下桌子:
.ExposeConfiguration(SetupTestDatabase)

...

private static void SetupTestDatabase(NHibernate.Cfg.Configuration config)
{
    var schema = new SchemaExport(config);
    schema.Drop(true, true);
    schema.Create(true, true);
}
它只是意味着您可以在不同的数据库上运行测试而无需更改其他任何内容。 编辑; woops;认为这是一个公认的解决方案。如果你在测试中这样做,就这样做:
   [Test]
    public void Test_can_store_and_get_objects()
    {
        var factory = CreateSessionFactory();
        using (var s = factory.OpenSession())
        { 
             ...
        }
    }

    private static ISessionFactory CreateSessionFactory()
    {
        return Fluently.Configure().Database(SQLiteConfiguration.Standard.UsingFile("firstProject.db"))
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Address>()) // <-- Refer to parent project
        .ExposeConfiguration(SetupTestDatabase)
        .BuildSessionFactory();
    }
    
这是我的一个例子。您需要使用
ExposeConfiguration
并传递一个接受配置的方法,您只需在那里构建数据库然后使用
SchemaExport
class SqliteRefSessionFactoryProvider : ISessionFactoryProvider
{

    public const string SqliteRefFileName = "ref.db";

    public ISessionFactory GetSessionFactory()
    {
        return Fluently.Configure().Database(
            SQLiteConfiguration.Standard.UsingFile(SqliteRefFileName).ShowSql())
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<SsoToken>())
            .ExposeConfiguration(BuildSchema)
            .BuildSessionFactory();
    }

    private static void BuildSchema(NHibernate.Cfg.Configuration configuration) 
    {
        if (File.Exists(SqliteRefFileName))
            File.Delete(SqliteRefFileName);

        new SchemaExport(configuration)
          .Create(false, true);
    }
}
    

要回复问题请先登录注册