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

标签 asp.net nhibernate fluent-nhibernate schemaexport

好吧,这是我第一个使用流利的 hibernate 的项目。我在 hibernate 和 nhibernate 方面的经验很少。

这个上下文对我来说是全新的,因为这是一个网络应用程序项目。 所以我有我的 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"/>

为了测试表的生成,我添加了一个测试项目(类库),添加了对 nunit.framework 2.8.5 和 myproject 的引用。

namespace myproject.Tests
{
  [TestFixture]
  public class CanGenerateSchemaTestSuite
  {
    [Test]
    public void CanGenarateSchema()
    {
       NhibernateSessionPerRequest.BuildSchema(NhibernateSessionPerRequest.GetCurrentSession());

     }
  }
}

测试方法总是失败,我遇到了这个异常:

CanGenerateSchemaTestSuite (1 test), 1 test failed: Child test failed CanGenarateSchema, Failed: 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);
}

这只是意味着您可以在不同的数据库上运行测试而无需更改任何其他内容。

编辑;哇哦;认为这是一个公认的解决方案。如果你在测试中这样做,就这样做:

   [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();
    }

关于asp.net - 如何使用流畅的 nhibernate (schemaexport) 测试生成表?在 asp.net 上下文中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4204173/

相关文章:

c# - 从 ASP.NET 网站下载动态生成的图像

asp.net - 当网站位于服务于 TLS 证书的负载均衡器后面时,将 cookie 标记为表单例份验证中的 "secure"

sql-server-2008 - 在未提交读的情况下使用 TransactionScope - SQL 中的 with (nolock) 是否必要?

c# - IManyToManyCollectionInstance 约定创建额外的表

c# - 带有子计数的 NHibernate 父列表

具有连接(或获取)的 Nhibernate 查询返回重复项

javascript - 如何使用 Jquery .load() 重新加载部分 View ?

asp.net - 如何找到控件的默认选项卡索引?

c# - NHibernate QueryOver 多个连接别名,只有第一个生成连接

c# - 有没有流利的 NHibernate 书?