5
votes

The domain:

public class BaseClassClient
{
    public virtual ICollection<BaseClass> BaseObjects{ get; set; }
}

public abstract class BaseClass
{
}

public class SubClass1 : BaseClass
{
}

public class SubClass2 : BaseClass
{
}

I get the error Association references unmapped class, even though both the sub classes are mapped. Obviously, the base class itself is not mapped. Please suggest how to solve this.

Edit

I would rather that before closing a question as a duplicate to another, they just not read, but also care to understand both the questions.

Ihave seen the question Error: fluent NHibernate mapping which references mapping in different assembly . But it talks about a different scenario.

My scenario is like below.

public class Product
{
    public int Id { get; set; }
    public ICollection<BasePricingRule> PricingRules{ get; set; }
}

public abstract class BasePricingRule
{
    public int Id { get; set; }
    //other properties
}

//Several concrete classes inherit from BasePricingRule.

I want to have one table per concrete class and no table for the base class. Therefore, there is no mapping for the BasePricingRule. I am allowing the classes to get auto mapped, occassionally providing necessary overrides. I created an automapping override for the product class as below.

    public void Override(FluentNHibernate.Automapping.AutoMapping<Product> mapping)
    {
        mapping.HasMany(product => product.PricingRules); //Not really sure
                                                          // about how to map this.
    } 

I have been seeing examples like http://ayende.com/blog/3941/nhibernate-mapping-inheritance for inheritance mappings. But these examples actually not address the issue I am facing. More than the error, I would like to know how I should map this domain via fluent NHibernate, preferably using automapping overrides.

1
please post your configuration, did you map assembly for fluent mapping? check this post stackoverflow.com/a/6227248/1225337Nikson Kanti Paul
Yes, I did. Type mapping is not the issue here. I haven't mapped the base class type, but only the concrete subclass types for which I wish to have a table. The link you point to does not address this inheritance issue.Victor Mukherjee

1 Answers

0
votes

The point is that, even for the Table Per Concrete Class approach, you will have to map the base class. Just not the way you may be thinking. You should take a look at this great article: www.codeproject.com/Articles/232034/Inheritance-mapping-strategies-in-Fluent-Nhibernat

It would be more or less like this:

public class BaseClassMap : ClassMap<BaseClass> {
    public BaseClassMap() {
        // indicates that this class is the base
        // one for the TPC inheritance strategy and that 
        // the values of its properties should
        // be united with the values of derived classes
        UseUnionSubclassForInheritanceMapping();
    }
}

public class SubClass1Map : SubclassMap<SubClass1> {
    public SubClass1Map() {
        Table("SubClass1Table");
        Abstract();
    }
}

public class SubClass2Map : SubclassMap<SubClass2> {
    public SubClass1Map() {
        Table("SubClass2Table");
        Abstract();
    }
}