Search code examples
c#nhibernate

NHibernate.InvalidProxyTypeException: The following types may not be used as proxies


I have an Entity which inherits from System.Web.Security.MembershipUser When I turn ON Lazy on the entity, I am receiving the following exception:

Exception Details: NHibernate.InvalidProxyTypeException: The following types may not be used as proxies:
CustomMembershipUser: method Update should be 'public/protected virtual' or 'protected internal virtual'
CustomMembershipUser: method GetPassword should be 'public/protected virtual' or 'protected internal virtual'
CustomMembershipUser: method ChangePassword should be 'public/protected virtual' or 'protected internal virtual'
CustomMembershipUser: method ResetPassword should be 'public/protected virtual' or 'protected internal virtual'

How can I fix this issue? Inside System.Web.Security.Membership:

        internal virtual void Update()
        {
            SystemWebProxy.Membership.Providers[ProviderName].UpdateUser(this);
            UpdateSelf();
        }

SO obviously I cannot add protected into methods inside MembershipUser


Solution

  • I don't think you're going to be able to use lazy loading with that type. NH's proxying relies on being able to create a derived type that inherits from the mapped type and intercepts all of its methods and properties. I think it does this using a third-party library, which is why it must need those methods to be virtual and with sufficient visibility.

    You can disable lazy loading for the type. Does it really offer you sufficient performance gains to justify its use?

    You could consider composing with System.Web.Security.Membership rather than inheriting from it?