Fluent NHibernate - override type for one specific property in one specific class?

I have a class that has a password property that I want to keep encrypted in db. The property is a string type, and I have my own EncryptedStringType type that I want to use NHibernate to map this to the database. Here is my corresponding autopilot code:

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() ); 

I cannot understand the syntax to override the type of UserPassword property for a business class. I thought I could do something with overrides like:

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

Any help is appreciated.

0
source share
2 answers

I found the answer myself.

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

You can always create a mapping override class. Any conventions that can still be applied will be, but you can basically specify mappings similar to ClassMap, which override the default conventions.

Using a call for comparisons. Back (), it will look something like this:

 mappings.Override<Business>(map=>map.Map(x=>x.UserPassword).CustomType(typeof(EncryptedStringType))); 
+1
source

Source: https://habr.com/ru/post/1307885/


All Articles