NHibernate InMemory测试

时间:2013-07-30 21:26:26

标签: unit-testing nhibernate in-memory-database

我正在尝试在NHibernate的内存测试中使用,我在这个小项目中成功地做到了: https://github.com/demojag/NHibernateInMemoryTest

从对象的地图中可以看出,我必须对此行进行评论:   // SchemaAction.None();测试将失败。此选项隐藏架构导出。

这个评论只是我想我已经做了,因为到目前为止我还没有找到关于Schema Actions的严肃文档。

我正在进行那些测试,因为我有一个现有的情况我想在内存中测试,但所有实体映射都有选项SchemaActions.None(),当我尝试执行内存测试时,我得到了很多“没有这样的表”。

我想知道是否存在将Schema操作选项设置为none并导出架构的方法? (我知道这可能是一个封装违规,所以它真的没有多大意义。)

我想将此选项设置为none,因为它是一个“DatabaseFirst”应用程序,我不会冒险丢弃数据库并在每次构建配置时重新创建它,但我想,如果在配置我没有指定指令“exposeConfiguration”和SchemaExport,我可以非常安全。

谢谢你的建议

朱塞佩。

1 个答案:

答案 0 :(得分:0)

您应该能够通过NHibernate.Cfg.Configuration.BeforeBindMapping事件覆盖HBM或Fluent NHibernate映射中的任何和所有设置,该事件为您提供对NHibernate内部模型的编程运行时访问以进行映射。请参阅下面的示例,该示例设置BeforeBindMapping事件处理程序,该处理程序将映射中指定的SchemaAction覆盖为您想要的任何内容。

public NHibernate.Cfg.Configuration BuildConfiguration()
{
    var configuration = new NHibernate.Cfg.Configuration();

    configuration.BeforeBindMapping += OnBeforeBindMapping;

    // A bunch of other stuff...

    return configuration;
}

private void OnBeforeBindMapping( object sender, NHibernate.Cfg.BindMappingEventArgs bindMappingEventArgs )
{
    // Set all mappings to use the fully qualified namespace to avoid class name collision
    bindMappingEventArgs.Mapping.autoimport = false;

    // Override the schema action to all
    foreach ( var item in bindMappingEventArgs.Mapping.Items )
    {
        var hbmClass = item as NHibernate.Cfg.MappingSchema.HbmClass;

        if ( hbmClass != null )
        {
            hbmClass.schemaaction = "all";
        }
    }
}
相关问题