0

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

正如您从对象的地图中看到的那样,我不得不评论这一行: //SchemaAction.None(); 测试将失败。此选项隐藏模式导出。

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

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

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

我想将此选项设置为无,因为它是一个“DatabaseFirst”应用程序,我不能冒险删除数据库并在每次构建配置时重新创建它,但我想,如果在配置中我不要指定指令“exposeConfiguration”和 SchemaExport,我可以很安全。

谢谢你的建议

朱塞佩。

4

1 回答 1

0

您应该能够通过事件覆盖 HBM 或 Fluent NHibernate 映射中的任何和所有设置,该NHibernate.Cfg.Configuration.BeforeBindMapping事件使您能够以编程方式运行时访问 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";
        }
    }
}
于 2013-07-31T03:18:26.010 回答