title: Fluent NHibernate Three-Level Sub-Class Mapping
body: <p>I have a complex NHibernate structure to store our 3 level product hierarchy. When I try and save at level 3 (ProductDetail2), I get the following error:</p>
  
  <blockquote>
    <p>Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [Domain.Models.ProductDetail2#193150]</p>
  </blockquote>
  
  <p>If I skip the level 3 save logic, it all goes through fine.</p>
  
  <p>Our class inheritance is shown below. ProductDetail1 and ProductDetail2 are identical, apart from having a different ProductType (for historical reasons)</p>
  
  <pre><code>public abstract class Product {
      public virtual int Id { get; set; }
      public virtual string Name { get; set; }
      public virtual int ProductTypeId { get; set; } 
      ...(other core product data)...
  }
  public class ProductDetail1 : Product {
      public string ShortDescription { get; set; }
      public override int ProductTypeId {
          get { return (int)ProductTypeEnum.ProductOne; }
      }
  }
  public class ProductDetail2 : ProductDetail1 {
      public override int ProductTypeId {
          get { return (int)ProductTypeEnum.ProductTwo; }
      }
  }
  </code></pre>
  
  <p>We are having to use two different types of subclass mappings, as per the definitions in <a href="https://github.com/jagregory/fluent-nhibernate/wiki/Fluent-mapping" rel="nofollow noreferrer">Fluent Mapping</a></p>
  
  <ul>
  <li>Product and ProductDetail1 save to different tables - Table-per-subclass Mapping</li>
  <li>ProductDetail1 and ProductDetail2 save to the same table - Table-per-hierarchy Mapping (This is because the only thing that distinguishes them is the ProductTypeId)</li>
  </ul>
  
  <p>Our Fluent mappings are as follows</p>
  
  <pre><code>public class ProductMap : ClassMap&lt;Product&gt; {   
    public ProductMap() {   
      Table("Product");
      Id(x =&gt; x.Id).GeneratedBy.Identity();
      Map(x =&gt; x.Name);
      DiscriminateSubClassesOnColumn("ProductTypeId", 0); 
    }
  }
  
  public class ProductDetail1Map : SubclassMap&lt;ProductDetail1&gt; {
      public ProductDetail1Map() {
          Table("ProductDetail"); 
          DiscriminatorValue((int)ProductTypeEnum.ProductOne);
          Join(
              "ProductOne", 
              y =&gt; {
                  y.KeyColumn("Id");
                  y.Map(x =&gt; x.ShortDescription);
              });
      }
  }
  
  public class ProductDetail2Map : SubclassMap&lt;ProductDetail2&gt; {
      public ProductDetail2Map() {
          DiscriminatorValue((int)ProductTypeEnum.ProductTwo);
      }
  }
  </code></pre>
  
  <p>Is this possible? If so, what am I doing wrong? </p>
  
  <p>P.S. I also found this similar <a href="https://stackoverflow.com/questions/16564348/fluent-nhibernate-two-levels-inheritance-issue">post</a>, but it differs from our case in that it defines (and saves) Discriminator column at level 1, whereas we define it at level 0.</p>