2011-03-31 46 views
0

我有一個類,有一個密碼屬性,我想在數據庫中加密存儲。該屬性是一個字符串類型,並且我有一個自定義類型EncryptedStringType,我希望NHibernate使用它將其映射到數據庫。這裏是我的相關自動映射代碼:流利的NHibernate - 重寫一個特定類的特定屬性的類型?

var mappings = AutoMap.AssemblyOf<Business>() 
    .Where(x=>x.IsSubclassOf(typeof(EntityBase))) 
    .IgnoreBase(typeof(EntityBase)) 
    .Conventions.Add 
     (
      ConventionBuilder.Id.Always(x => 
       x.GeneratedBy.HiLo(HILO_TABLE, HILO_COLUMN, HILO_MAX_LO)), 
      ConventionBuilder.HasMany.Always(x => x.Cascade.AllDeleteOrphan()), 
      Table.Is(o => Inflector.Pluralize(o.EntityType.Name)), 
      PrimaryKey.Name.Is(o => "Id"), 
      ForeignKey.EndsWith("Id"), 
      DefaultLazy.Always(), 
      DefaultCascade.All() 
     ); 

我想不通的語法雖然覆蓋了企業級的的userPassword屬性的類型。我認爲我應該能夠做一些重寫,如:

mappings.Override<Business>(map=> /* Not sure what to do here */); 

任何幫助表示讚賞。

回答

0

我自己找到了答案。

mappings.Override<Business>(map => 
{ 
    map.Map(x => x.UserPassword).CustomType<EncryptedStringType>(); 
}); 
1

你總是可以創建一個映射覆蓋類。仍然可以應用的任何約定都是,但基本上可以指定類似於覆蓋默認約定的ClassMap的映射。

使用調用mappings.Override(),它會看起來像:

mappings.Override<Business>(map=>map.Map(x=>x.UserPassword).CustomType(typeof(EncryptedStringType))); 
相關問題