Chris
Chris

Reputation: 28064

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

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.

Upvotes: 0

Views: 1195

Answers (2)

KeithS
KeithS

Reputation: 71565

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

Upvotes: 1

Chris
Chris

Reputation: 28064

Found the answer myself.

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

Upvotes: 1

Related Questions