Monday 19 October 2009

NHibernate - Entity property is an Interface - mapping

If you have a class that maps to a table and a property of the class is an association to another table (i.e. 1:M Property) you may get the following error if the property is defined as an interface type:

 

NHibernate An association from the table XYZ refers to an unmapped class

 

This occurs because NHibernate will not have a mapping file for the interface type and cannot instantiate it because it is an interface. To fix the problem you need to include in the mapping file the actual class the property represents.

e.g.

 

IAccount MainAccount;

 

may map to

 

<many-to-one name="MainAccount" column="MainAccountID" class="BankingCore.BusinessEntities.Account, BankingCore"/>

 

where BankingCore.BusinessEntities.Account is the actual class to be instantiated for the property.