Reputation: 3412
Is there any way to set name for unique index when generating schema with SchemaExport(config).Build(true, true)
?
I tried to set in mapping class:
Map(x => x.Name)
.Length(50)
.Not.Nullable()
.UniqueKey("UNQ_InstitutionTypes_Name");
But, this way it sets index, but doesn't sets name.
Upvotes: 1
Views: 1052
Reputation: 2387
As far as I know there isn't a way. I solved this problem using following technique.
When you build the SessionFactory use the ExposeConfiguration method to apply additional configuration to your session factory:
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);
}
The actual Indexes.hbm.xml file looks like this:
<?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>
You can put any valid SQL statements inside the create/drop statements. This comes in handy if you need to create index with multiple columns in specific order.
Upvotes: 1