2015-04-30 32 views
0

做實體的我設置ID與ConventionBuilder我有以下流利配置如何在功能NHibernate

var sessionFactory = Fluently.Configure() 
          .Database(configuration) 
          .Mappings(arg => 
          { 
           var autoMap = AutoMap.Source(typeSource); 
           foreach (var convention in typeSource.GetConventions()) 
           { 
            autoMap.Conventions.Add(convention); 
           } 
           autoMap.BuildMappings(); 
          }) 
          .BuildSessionFactory(); 

我用的FluentNHibernate.ITypeSource也有一個方法GetConventions,它返回System.Collections.Generic.IEnumerable<FluentNHibernate.Conventions.IConvention>一個實例(typeSource)。

GetConventions的結果然後被用於注入到FluentNHibernate.Automapping.AutomapConventions(在流暢配置的方法Mappings中)。

GetConventions具體實現看起來像

public override System.Collections.Generic.IEnumerable<FluentNHibernate.Conventions.IConvention> GetConventions() 
{ 
    yield return FluentNHibernate.Conventions.Helpers.ConventionBuilder.Property.When(
      arg => arg.Expect(f => f.Type == typeof (string)), 
      arg => arg.CustomType<CUSTOMTYPE>() 
    ); 
} 

我也想介紹一個約定注入基礎上,EntityType一個ID,我已經嘗試做這樣的:

yield return FluentNHibernate.Conventions.Helpers.ConventionBuilder.Class.When(
     arg => arg.Expect(f => f.EntityType == typeof (ENTITYTYPE)), 
     arg => arg.Id // does not work, as .Id is readonly 
); 

那麼,如何在ConventionBuilder中注入ID,也可能是CombinedID?

編輯:

我也試了這一點,但遺憾的是在應用路徑(var a = 1;)斷點永遠不會達到:

yield return FluentNHibernate.Conventions.Helpers.ConventionBuilder.Id.When(
     arg => arg.Expect(f => f.EntityType == typeof(ENTITYTYPE)), 
     arg => 
     { 
      var a = 1; 
     } 
); 

回答

0

我解決了這個通過擴展Mappings -call一個位:

.Mappings(arg => 
{ 
    var autoPersistenceModel = AutoMap.Source(typeSource); 
    foreach (var overrideType in typeSource.GetOverrideTypes()) 
    { 
     autoPersistenceModel.Override(overrideType); 
    } 
    foreach (var conventionType in typeSource.GetConventionTypes()) 
    { 
     autoPersistenceModel.Conventions.Add(conventionType); 
    } 
    arg.AutoMappings.Add(autoPersistenceModel); 
}) 

不幸的是,這是遠離優雅或完整 - 這將是方式更體面,如果AutoMap.Source(typeSource)可以處理conven從GetTypes-方法或如果IIdConvention將被考慮在地圖上(它不atm)...