【发布时间】:2013-02-14 12:16:40
【问题描述】:
当我在我的呼吸系统代码上运行测试时,会删除并创建表,并添加新数据以用于测试
new SchemaExport(_configuration).Execute(false, true, false);
但是它强制引用完整性,在生产中这会很好,但在测试中我要求不要启用它。
用上面的代码创建表格时有什么方法可以禁用它们吗?
【问题讨论】:
标签: nhibernate nunit schemaexport
当我在我的呼吸系统代码上运行测试时,会删除并创建表,并添加新数据以用于测试
new SchemaExport(_configuration).Execute(false, true, false);
但是它强制引用完整性,在生产中这会很好,但在测试中我要求不要启用它。
用上面的代码创建表格时有什么方法可以禁用它们吗?
【问题讨论】:
标签: nhibernate nunit schemaexport
使用 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"
}
【讨论】: