title: NHibernate: Object with id was not of the specified subclass: (loading object was of wrong class )
body: <p>I am getting the error below:</p>
  
  <p><strong>Object with id: 34dd93d3-374e-df11-9667-001aa03fa2c4 was not of the specified subclass: MyNamespace.ClassA (loading object was of wrong class [MyNamespace.ClassB1])</strong></p>
  
  <p>Call stack is:</p>
  
  <p><em>at NHibernate.Loader.Loader.InstanceAlreadyLoaded(IDataReader rs, Int32 i, IEntityPersister persister, EntityKey key, Object obj, LockMode lockMode, ISessionImplementor session)<br>
     at NHibernate.Loader.Loader.GetRow(IDataReader rs, ILoadable[] persisters, EntityKey[] keys, Object optionalObject, EntityKey optionalObjectKey, LockMode[] lockModes, IList hydratedObjects, ISessionImplementor session)<br>
     at NHibernate.Loader.Loader.GetRowFromResultSet(IDataReader resultSet, ISessionImplementor session, QueryParameters queryParameters, LockMode[] lockModeArray, EntityKey optionalObjectKey, IList hydratedObjects, EntityKey[] keys, Boolean returnProxies)<br>
     at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)<br>
     at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)<br>
     ...<br>
     at NHibernate.Linq.CriteriaResultReader<code>1.&lt;GetEnumerator&gt;d__0.MoveNext()<br>
     at System.Collections.Generic.List</code>1..ctor(IEnumerable<code>1 collection)<br>
     at System.Linq.Enumerable.ToList[TSource](IEnumerable</code>1 source)<br>
     …</em></p>
  
  <p>I have renamed my classes and took out the irrelevant properties and such, to simplify, but the levels of inheritance are exactly like in my real scenario.</p>
  
  <p>The classes:</p>
  
  <pre><code>public abstract class BaseClass : 
  {
      public Guid ID { get; set; }
      ...
  
  }
  public abstract class ClassA : BaseClass
  {
      public ClassB RelatedObject { get; set; }
  }
  public abstract class ClassB : BaseClass
  {
      public ClassA RelatedObject { get; set; }
  }
  public abstract class ClassC : BaseClass
  {
      ...
  }
  
  public class ClassA1 : ClassA
  {
      ...
  }
  public class ClassA2 : ClassA
  {
      ...
  }
  
  public class ClassB1 : ClassB
  {
      ...
  }
  public class ClassB2 : ClassB
  {
      ...
  }
  
  public class ClassC1 : ClassC
  {
      ...
  }
  public class ClassC2 : ClassC
  {
      ...
  }
  </code></pre>
  
  <p>There  is a table for the base class, and then individual tables for some of the concrete classes, not all of them. The corresponding column for the related object is in the main table.</p>
  
  <pre><code>BaseClassTable:
  -------------------------------
  | ID | Kind | RelatedObjectID |
  -------------------------------
  
  Individual tables:
  ---------------------------------
  | ID | some specific properties |
  ---------------------------------
  </code></pre>
  
  <p>Mapping is:</p>
  
  <pre><code>&lt;class name="MyNamespace.BaseClass, MyNamespace" table="BaseClassTable" discriminator-value="BC" abstract="true"&gt;
      &lt;id name="ID" type="System.Guid" column="ID" unsaved-value="00000000-0000-0000-0000-000000000000"&gt;
        &lt;generator class="guid" /&gt;
      &lt;/id&gt;
      &lt;discriminator column="Kind" type="System.String"/&gt;
      ...
      &lt;subclass name="MyNamespace.ClassA, MyNamespace" discriminator-value="A" abstract="true"&gt;
          &lt;many-to-one name="RelatedObject" class="MyNamespace.ClassB, MyNamespace" column="RelatedObjectID"/&gt;
      &lt;/subclass&gt;
      &lt;subclass name="MyNamespace.ClassB, MyNamespace" discriminator-value="B" abstract="true"&gt;
          &lt;many-to-one name="RelatedObject" class="MyNamespace.ClassA, MyNamespace" column="RelatedObjectID"/&gt;
      &lt;/subclass&gt;
      &lt;subclass name="MyNamespace.ClassC, MyNamespace" discriminator-value="C" abstract="true"&gt;
      ...
      &lt;/subclass&gt;
    &lt;/class&gt;
    &lt;subclass name="MyNamespace.ClassA1, MyNamespace" extends="MyNamespace.ClassA, MyNamespace" discriminator-value="A1"&gt;
      &lt;join table="ClassA1Table"&gt;
          &lt;key column="ID"/&gt;
          ...
      &lt;/join&gt; 
      ...
    &lt;/subclass&gt;
    &lt;subclass name="MyNamespace.ClassA2, MyNamespace" extends="MyNamespace.ClassA, MyNamespace" discriminator-value="A2"&gt;
      ...
    &lt;/subclass&gt;
    &lt;subclass name="MyNamespace.ClassB1, MyNamespace" extends="MyNamespace.ClassB, MyNamespace" discriminator-value="B1"&gt;
      ...
    &lt;/subclass&gt;
    &lt;subclass name="MyNamespace.ClassB2, MyNamespace" extends="MyNamespace.ClassB, MyNamespace" discriminator-value="B2"&gt;
      ...
    &lt;/subclass&gt;
    &lt;subclass name="MyNamespace.ClassC1, MyNamespace" extends="MyNamespace.ClassC, MyNamespace" discriminator-value="C1"&gt;
      ...
        &lt;/subclass&gt;
    &lt;subclass name="MyNamespace.ClassC2, MyNamespace" extends="MyNamespace.ClassC, MyNamespace" discriminator-value="C2"&gt;
      ...
    &lt;/subclass&gt;
  </code></pre>
  
  <p>Of course, in the DB I’m only going to have the A1, A2, B1, B2, C1 and C2 values for the discriminator.</p>
  
  <p>Did anybody run into something like this before? Can a property be mapped as a base class and then have an instance of the concrete class loaded, for the same ID? Is there a better way to accomplish this? 
  Thank you!</p>
  
  <p>This is the call that throws the exception:</p>
  
  <p>The error is thrown when trying to retrieve objects using the IQueryable from my data context:</p>
  
  <pre><code>List&lt;MyNamespace.BaseClass&gt; items = context.MyObjects.ToList();
  </code></pre>
  
  <p>And in my data context, the IQueryable property looks like this:</p>
  
  <pre><code>IQueryable&lt;MyNamespace.BaseClass&gt; IMyDataContext.MyObjects
  {
     get
     {
        return this.Session.Linq&lt;MyNamespace.BaseClass&gt;();
     }
  }
  </code></pre>