有什麼辦法可以在生成架構時使用SchemaExport(config).Build(true, true)
來設置唯一索引的名稱?有沒有辦法在Fluent nHibernate中命名唯一索引?
我試圖映射類設置:
Map(x => x.Name)
.Length(50)
.Not.Nullable()
.UniqueKey("UNQ_InstitutionTypes_Name");
但是,這樣一來它集指數,但不集名稱。
有什麼辦法可以在生成架構時使用SchemaExport(config).Build(true, true)
來設置唯一索引的名稱?有沒有辦法在Fluent nHibernate中命名唯一索引?
我試圖映射類設置:
Map(x => x.Name)
.Length(50)
.Not.Nullable()
.UniqueKey("UNQ_InstitutionTypes_Name");
但是,這樣一來它集指數,但不集名稱。
據我所知,沒有辦法。我使用以下技術解決了這個問題。
當您生成SessionFactory的使用ExposeConfiguration方法來應用額外的配置你的會話工廠:
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語句。如果您需要按特定順序創建具有多個列的索引,這會派上用場。
也許這個答案是正確的,但這不是我想要在實踐中使用的。模式生成是很好的功能,但不是100%可用 - 大部分必須手工完成(或附加腳本):UNQ索引名稱,檢查約束等。相似性對象 - 工作太多,影響小。對於我的問題,我認爲,最好的答案是在這個鏈接:http://fabiomaulo.blogspot.com/2011/07/nhibernate-playing-with-mapping-by-code.html – VikciaR 2012-01-01 08:29:04
在格式化跨越多行的代碼時,您需要在每行前加上四個空格。反引號僅適用於佔用一行的代碼。看我的編輯。 – Amy 2011-12-27 01:20:22