簡體   English   中英

如何在Fluent NHibernate中使用ConventionBuilder設置實體ID

[英]How do I setup Id of entity with a ConventionBuilder in Fluent 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的實例( typeSource ),它也具有方法GetConventions ,該方法返回System.Collections.Generic.IEnumerable<FluentNHibernate.Conventions.IConvention>

然后,將GetConventions的結果用於FluentNHibernate.Automapping.Automap Conventions中(在Fluent配置的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?

編輯:

我也嘗試過這種方法,但可悲的是,apply-path( var a = 1; )中的斷點從未達到:

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

我通過擴展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)可以從手柄公約和覆蓋GetTypes -方法或者IIdConvention將考慮建設地圖(它不會自動櫃員機)...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM