【问题标题】:SchemaExport and turning off referential integrity constraintSchemaExport 和关闭参照完整性约束
【发布时间】:2013-02-14 12:16:40
【问题描述】:

当我在我的呼吸系统代码上运行测试时,会删除并创建表,并添加新数据以用于测试

new SchemaExport(_configuration).Execute(false, true, false);

但是它强制引用完整性,在生产中这会很好,但在测试中我要求不要启用它。

用上面的代码创建表格时有什么方法可以禁用它们吗?

【问题讨论】:

    标签: nhibernate nunit schemaexport


    【解决方案1】:

    使用 FluentNHibernate 只是为测试添加此约定

    public class NoForeignKeys : IReferenceConvention, IHasManyConvention
    {
        public void Apply(IManyToOneInstance instance)
        {
            instance.ForeignKey("none");
        }
    
        public void Apply(IOneToManyCollectionInstance instance)
        {
            instance.Key.ForeignKey("none");
        }
    }
    

    使用普通的 NHibernate,您需要遍历所有映射的类属性并在那里进行更改。

    foreach (var prop in config.ClassMappings.SelectMany(c => c.PropertyClosureIterator).Where(p => p.IsEntityRelation || <is hasmany>))
    {
         // set foreignkey name to "none"
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-04
      • 2019-07-25
      • 1970-01-01
      • 1970-01-01
      • 2011-03-13
      相关资源
      最近更新 更多