2011-12-26 58 views
1

有什么办法可以在生成架构时使用SchemaExport(config).Build(true, true)来设置唯一索引的名称?有没有办法在Fluent nHibernate中命名唯一索引?

我试图映射类设置:

Map(x => x.Name) 
    .Length(50) 
    .Not.Nullable() 
    .UniqueKey("UNQ_InstitutionTypes_Name"); 

但是,这样一来它集指数,但不集名称。

+0

在格式化跨越多行的代码时,您需要在每行前加上四个空格。反引号仅适用于占用一行的代码。看我的编辑。 – Amy 2011-12-27 01:20:22

回答

1

据我所知,没有办法。我使用以下技术解决了这个问题。

当您生成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语句。如果您需要按特定顺序创建具有多个列的索引,这会派上用场。

+0

也许这个答案是正确的,但这不是我想要在实践中使用的。模式生成是很好的功能,但不是100%可用 - 大部分必须手工完成(或附加脚本):UNQ索引名称,检查约束等。相似性对象 - 工作太多,影响小。对于我的问题,我认为,最好的答案是在这个链接:http://fabiomaulo.blogspot.com/2011/07/nhibernate-playing-with-mapping-by-code.html – VikciaR 2012-01-01 08:29:04

相关问题