是否有一种流畅的NHibernate方式来索引所有外键而无需编码?

时间:2013-01-17 03:14:29

标签: c# nhibernate fluent-nhibernate conventions

这可能吗,可能会有约定吗?我有数百个表和无数的关系,我希望所有外键都被编入索引,我不想编写代码。

更新

在@ Vadim的帮助下,我能够做到这一点:

var fkIndexConvention = ConventionBuilder.Reference.Always(x =>
    x.Index(string.Format("ix{0}_{1}_{2}", x.Class.Name, x.Property.Name,
    Guid.NewGuid().ToString().Replace("-", string.Empty))));

...然后将其添加到我的流畅配置中:

var fluent = Fluently.Configure(config)
    .Mappings(m => m.FluentMappings.Conventions.Add(fkIndexConvention));

......世界是个更好的地方。

1 个答案:

答案 0 :(得分:2)

我假设您正在讨论使用hbm2ddl工具和SchemaExport。

您需要做的就是设置参考约定。由于IManyToOneInstance隐藏了外键,因此您应该能够将实例强制转换为IManyToOneInspector

ConventionBuilder.Reference.Always(x => x.Index(((IManyToOneInspector)x).ForeignKey + "Index"));
相关问题