Fluent NHibernate CheckReference Throws Expected ‘X’ but Got ‘XProxy’

· September 8, 2009

Lately, I’ve been working extensively with Fluent NHibernate and encountered a recurring issue.

When setting up a mapping test with PersistenceSpecification<T> to test a reference (using CheckReference, for instance), I often get the following exception:

System.ApplicationException: Expected ‘Marcusoft.Product' but got 'ProductProxy…' for Property 'Product'

This issue arises because NHibernate generates a proxy class, which causes problems when performing equality tests on that class. To address this, I found a base class that resolves this problem, provided by the Hibernating Rhinos (what a name!).

You can read more about this solution here or check out my code here.

Twitter, Facebook