C# – NHibernate mapping error – invalid child element ‘many-to-one’

cfluent-nhibernatenhibernate

Using Fluent NHibernate with automapping, I am trying to map the following domain:

public class Company: IModel
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}

public class Account: IModel
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual Company Company { get; set; }
}

One Company can have many accounts. In general I will get an account directly and then occasionally want to find the associated Company, so there's no need for a list of accounts on the company model.

Fluent NHibernate creates the following hbms:

Company

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
  <class xmlns="urn:nhibernate-mapping-2.2" name="DataModel.Company, DataModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Company`">
    <id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Id" />
      <generator class="identity" />
    </id>
    <property name="Name" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Name" />
    </property>
  </class>
</hibernate-mapping>

Account

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
  <class xmlns="urn:nhibernate-mapping-2.2" name="DataModel.Account, DataModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Account`">
    <id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Id" />
      <generator class="identity" />
    </id>
    <property name="Name" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Name" />
    </property>
    <many-to-one class="DataModel.Company, DataModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Company">
      <column name="Company_id" />
    </many-to-one>
  </class>
</hibernate-mapping>

And I am getting the following error:

The element 'class' in namespace
'urn:nhibernate-mapping-2.2' has
invalid child element 'many-to-one' in
namespace
'urn:nhibernate-mapping-2.2'. List of
possible elements expected: 'meta,
subselect, cache, synchronize,
comment, tuplizer, id, composite-id'
in namespace
'urn:nhibernate-mapping-2.2'.

What do I need to do to make this a valid mapping?

Best Answer

The problem turned out not to be with these domain classes. I had a Repository class in the same assembly, which Fluent NHibernate was apparently trying to include in the domain. It was the attempted mapping of this class which caused the error (unfortunately the error message didn't say which type caused the problem).

I fixed it by adding a where constraint on the namespace.

Related Topic