有没有办法命名唯一索引在Fluent nHibernate
本文关键字:索引 Fluent nHibernate 唯一 有没有 | 更新日期: 2023-09-27 18:18:54
在使用SchemaExport(config).Build(true, true)
生成模式时是否有办法为唯一索引设置名称?
我尝试在映射类中设置:
Map(x => x.Name)
.Length(50)
.Not.Nullable()
.UniqueKey("UNQ_InstitutionTypes_Name");
但是,这样它设置索引,但不设置名称。
据我所知没有办法。我用下面的方法解决了这个问题。
当您构建SessionFactory时,使用exposeconconfiguration方法将额外的配置应用到会话工厂:
return Fluently.Configure()
.ProxyFactoryFactory(typeof(ProxyFactoryFactory))
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(connectionString)
.Mappings(m =>
{
m.FluentMappings.AddFromAssemblyOf<Entities.BaseEntity>();
m.FluentMappings.Conventions.AddFromAssemblyOf<Entities.BaseEntity>();
})
.ExposeConfiguration((cfg => BuildDatabase(cfg)))
.BuildSessionFactory();
private static void BuildDatabase(Configuration cfg, IDatabaseConfiguration configurationManager)
{
cfg.AddXmlFile(@"Mappings'Hbm'Indexes.hbm.xml");
new SchemaExport(cfg).SetOutputFile(SchemaOutputFileName).Create(false, false);
}
实际的Indexes.hbm.xml文件如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<database-object>
<create>
CREATE NONCLUSTERED INDEX [Idx_TestRun_SerialNumber] ON [dbo].[TestRun]
(
[SerialNumber] ASC
)
</create>
<drop></drop>
</database-object>
</hibernate-mapping>
您可以将任何有效的SQL语句放入create/drop语句中。如果您需要以特定顺序创建包含多个列的索引,这将非常方便。