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

人走茶凉 提交于 2020-01-06 05:49:09

问题


I have a class that has a password property that I want to store encrypted in the db. The property is a string type, and I have a custom type EncryptedStringType that I want NHibernate to use to map this to the database. Here is my relevant automapping 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 figure out the syntax to override the type for the UserPassword property of the Business class though. I thought I should be able to do something with overrides like:

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

Any help is appreciated.


回答1:


Found the answer myself.

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



回答2:


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

Using the call to mappings.Override(), it'd look something like:

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


来源:https://stackoverflow.com/questions/5506524/fluent-nhibernate-override-type-for-one-specific-property-on-one-specific-clas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!