Search code examples
nhibernatenhibernate-mapping-by-code

Ignore column using mapping by code in HNibernate


I'm using mapping by code in NHibernate. I got a class with several properties. One of them is not related to any columns in DB but still has getter and setter.

I use ConventionModelMapper not ModelMapper. The first one assumes that all properties are mapped.

How i can tell to NHibernate to ignore it?


Solution

  • Why not map the properties you want and leave the ones not needed to be mapped

    check this

    You can manage the persistence of ConventionModelMapper as following:

    mapper.BeforeMapProperty += (mi, propertyPath, map) =>
    {
        // Your code here using mi, propertyPath, and map to decide if you want to skip the property .. can check for property name and entity name if you want to ignore it
    };
    

    A better answer would be:

    mapper.IsPersistentProperty((mi, declared) =>
                                                 {
                                                     if (mi.DeclaringType == typeof (YourType) && mi.Name == "PropertyNameToIgnore")
                                                         return false;
                                                     return true;
                                                 });