Search code examples
fluent-nhibernateautomapping

Using Fluent NHiberanate how do I turn off the mapping of a non persistent property


All my persistent objects have a property which should not be persisted.

At this moment i generate my automapping like this:

var autoMap = 
     AutoMap.AssemblyOf<BaseEntity>()
    .Where(type => type.Namespace != null && type.Namespace.Contains("Models"))
    .Conventions.AddFromAssemblyOf<IEntity>()
    .OverrideAll(map => map.IgnoreProperty("IsDummy")); 

However the following error is returned:

System.TypeInitializationException: System.TypeInitializationException: The type initializer for 'Core.Context' threw an exception. ---> NHibernate.InvalidProxyTypeException: The following types may not be used as proxies: Core.Models.MyEntity: method get_IsDummy should be 'public/protected virtual' or 'protected internal virtual'

This leads me to believe that the override did not work. (Core.Context is the class triggering the mapping process)


Solution

  • You have to make a property virtual, even if it not mapped. Otherwise, NHibernate can't properly generate proxy for lazy loading your object.