3

I am using Fluent NHibernate to map out an existing database. For this reason - automapping isn't an option for me.

How do I tell NHibernate not to map certain properties? Many of them are read-only, and the others do not need to be persisted for other reasons.

I am writing this in VB.Net.

I get the typical error message: "The following types may not be used as proxies ... should be 'public/protected virtual' or 'protected internal virtual'"

I have purposely not made my objects Overridable (equivalent to virtual in C#) because I do not want NHibernate to touch them.

How can I achieve this?

Origin
  • 1,943
  • 13
  • 33
  • I am still interested in a less hack-ish answer, but I found a workaround at this post: http://stackoverflow.com/questions/907576/how-to-tell-fluent-nhibernate-not-to-map-a-class-property The question marked as the answer only applies to AutoMapping, and not ClassMapping. If you read the comments carefully though, at the bottom they mention just making the class Virtual (overridable). At first this seems strange, but the very last comment mentions that doing so has no adverse effects as long as you don't try to map to that property. It will simply create a proxy it will never use. – Origin Dec 21 '11 at 06:35
  • 2
    [Here](http://stackoverflow.com/questions/2278110/nhibernate-exception-method-add-should-be-public-protected-virtual-or-protec) is a good answer about why you need to make all methods (and of course properties) virtual. This is about NHibernate and not about FluentNHibernate. – Michele Lepri Dec 21 '11 at 11:33

1 Answers1

7

All properties and methods must be overridable in order for NHibernate to create dynamic proxies, including unmapped properties. This does not imply that NHibernate is mapping your read-only properties, it just requires them to be overridable so that it can generate a proxy of the class. This article explains the requirement.

Jamie Ide
  • 48,427
  • 16
  • 81
  • 117
  • Thanks for the response. I had since been told this by someone else as well but forgot to update it here. – Origin Dec 23 '11 at 03:51
  • This is very unfortunate because I have unmapped properties that I don't want overridden or changed :-/ but oh, well. The answer helped, thanks. – Nick Williams Jan 16 '14 at 15:48