Stack Exchange
nHibernate Per table class map, formula reference parent preoperty
TL;DR
Take the "Subclasses" example from here 
https://github.com/jagregory/fluent-nhibernate/wiki/Fluent-mapping.  
I want to have a formula in ChildMap that uses the Name property of Parent
Long Details
I have an inheritance hierarchy (Product -> StockProduct, KitProduct, ListingProduct, VariationParentProduct) with an Inventory_Product table holding all the common fields, and Inventory_StockProduct/Inventory_KitProduct etc with the specific fields for the sub-class.  I believe this is called sub-class table mapping.  
On my base class Product I have a property called ProductType.  
public abstract class Product
{
    public virtual Guid ProductID { get; set; }

    public abstract ProductType ProductType { get; }

    public virtual int AvailableQuantity { get; set; }
}

public class StockProduct : Product
{
    // Some other fields
    public override ProductType ProductType
    {
        get { return ProductType.Stock; }
    }
}

public class KitProduct : Product
{
    // Some other fields
    public override ProductType ProductType
    {
        get { return ProductType.Kit; }
    }
}

public class ListingProduct : Product
{
    // Some other fields
    public override ProductType ProductType
    {
        get { return ProductType.Listing; }
    }
}

public class VariationParentProduct : Product
{
    // Some other fields
    public override ProductType ProductType
    {
        get { return ProductType.VariationParent; }
    }
}

public enum ProductType
{
    Stock = 0,
    Kit = 1,
    Listing = 2,
    VariationParent = 3
}

And the mappings
public class ProductMap : ClassMap<Product>
{
   public ProductMap()
    {
        Table("Inventory_Products");

        Id(x => x.ProductID).GeneratedBy.Guid();
        Map(x => x.Sku);

        Map(x => x.ProductType).CustomType<ProductType>().Access.ReadOnly();
    }
}

public class StockProductMap : SubclassMap<StockProduct>
{
    public StockProductMap()
    {
        Table("Inventory_StockProducts");

        KeyColumn("ProductID");

        Map(x => x.AvailableQuantity).Formula("(CASE ProductType WHEN 0 THEN (1) ELSE NULL END)");
    }
}

public class KitProductMap : SubclassMap<KitProduct>
{
    public KitProductMap ()
    {
        Table("Inventory_KitProducts");

        KeyColumn("ProductID");

        Map(x => x.AvailableQuantity).Formula("(CASE ProductType WHEN 0 THEN (2) ELSE NULL END)");
    }
}

I need to somehow get access to this in a formula defined on the StockProduct/KitProduct to do different SQL 
I have tried on StockProduct
Map(x => x.AvailableQuantity).Formula("(CASE ProductType WHEN 0 THEN (1) ELSE NULL END)") 

and KitProduct
Map(x => x.AvailableQuantity).Formula("(CASE ProductType WHEN 1 THEN (2) ELSE NULL END)") 

But nhibernate prefixes ProductType with the table alias for [KitProduct|StockProduct] rather than the base product. So the column its referencing doesn't exist
Is there an equivalent to this. in a formula for the parent object?

ProductType can be 1 or 0?

Put your whole map and classes in question.

Please post example code so we can help you.  Also, in the future be sure to mark your code as code to make it more readable!

Sorry, first ever post to SO, not sure on the rules :)  Updating the post now

In my experience it's best to enclose table names / columns in [] to prevent NHibernate modifying them in raw sql, e.g. (CASE [ProductType] WHEN 1 THEN (1) ELSE NULL END)

@JLevett when I do that it just links the field agaisnt Inventory_KitProducts rather than Inventory_Products

@StormeHawke sorry, updated now with more details

@user2586804 sorry, updated now with more details

In that case you can prefix the table name [Inventory_Products].[ProductType]

@JLevett that doesn't work because nHibernate is changing the alias to [Inventory_Products]. to product_0 (or other) and I don't want to rely on some hard coded magic string in my formula

I don't get it, you know the product type as you're doing the mapping for that product type, why the need for a test on base product type?

I think it's not a good design to have both a class hierarchy and a property to tell you what concrete class it is. You certainly don't need this column to implement Table per Class.
You should use this in the base class:
UseUnionSubclassForInheritanceMapping();

And this in the sub classes mappings:
Abstract();

However, I think you are trying to achieve Table per Type
See this article for a good explaination of the three different mapping strategies: http://www.codeproject.com/Articles/232034/Inheritance-mapping-strategies-in-Fluent-Nhibernat

I understand the differences.  Maybe I should explain the underlying problem I'm trying to solve.
I don't need the Type column, I was just trying to use it to solve my ultimate problem.  I want different SQL to be run for the AvailableQuantity column for each subclass.
By default nHibernate runs the SQL for each subclass type for each subclass, then only uses the relevant SQL to populate the object
This SQL could be quite expensive and so it's inefficient to run the SQL for ListingProduct, VariationParentProduct and KitProduct when all I want to do is have the value for my StockProduct

I forgot to add, the unionsubclass thing seems very inefficient.  if i want to do a query on a field that is on the parent class then it need to do a union.

@AndrewMurphy Well it's not inefficient in my experience, I use it on some huge tables, as long as the keys are present on both tables, it runs amazingly fast. I do not understand what different SQL you want to run for the two sub classes, as your example was all about fixed values, and you can providing a different mapping for AvailableQuantity in the sub classes, so why is that not good enough?
