Dezorian Dezorian -3 years ago 146 Question

Ignoring properties in Fluent NHibernate with VB.Net (2.0)

Because there is so little information about VB.Net and (Fluent) NHibernate to be found, I decided to write this question to all other developers finding themselves looking for more information.

One of the things I had to struggle with was how to Ignore properties in NHibernate.

The reason I had to ignore properties was because we used a Webserivce which cannot serialize Interface classes (ILists). Which are used a lot with NHibernate.

So we had to ignore some properties from NHibernate and let those properties convert the IList objects to List objects which could be used in Webservice.

We couldn't find any good translation from this C# code to VB.Net:

.Override<Shelf>(map =>
map.IgnoreProperty(x => x.YourProperty);


.OverrideAll(map =>

And found a different solution to solve the problem (see self-made answer)

Answer Source

Make a new class which implements DefaultAutomappingConfiguration

Imports FluentNHibernate.Automapping

Public Class AutomapperConvention
    Inherits DefaultAutomappingConfiguration

    Public Overrides Function ShouldMap(ByVal member As FluentNHibernate.Member) As Boolean
        'When the the mapper finds a List object it ignores the mapping you can make your own choices here'
        If (member.PropertyType.IsGenericType AndAlso Not (member.PropertyType.IsInterface)) Then
            Return False
            Return MyBase.ShouldMap(member)
        End If
    End Function

End Class

Add the AutomapperConvention here:

'Custom automapping to make the automapper find the correct id's (entityname + "Id")
Dim mappingConfig As New AutomapperConvention()
Dim model As New FluentNHibernate.Automapping.AutoPersistenceModel(mappingConfig)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download