Question

I have a Base class and two childs (A:Base and B:Base), and I want to map them to two tables (table A and table B). Is that possible in Fluent NHibernate? So I have:

public class Base
{
    public virtual int Id {get;set;}
    public virtual int IndexIn {get;set;}
    public virtual Product Product {get;set;}
}

public class A : Base
{
    public virtual string Value {get;set;}
}

public class B : Base
{
    public virtual int Value {get;set;}
    public virtual IList<Sequence> Sequences {get;set;}
}

My mapping is:

public class BaseMap : ClassMap<Base>
{
     public BaseMap()
     {
        Id(x => x.Id);
        Map(x => x.IndexIn);
        References(x => x.Product);
     }
}

public class AMap : SubclassMap<A>
{
     public AMap()
     {
         Map(x => x.Value);
     }
}
public class BMap : SubclassMap<B>
{
     public BMap()
     {
         Map(x => x.Value);
         HasMany(x => x.Sequences);
     }
}

But in that case it creates three tables (A, B and Base). That's nice, but I need to reduce number of tables, so I am ok to have Base's fields in both A and B table. Generally I want to simply map A and B as normal classes (without using inheritance), but I need to be able to add some other class, where I can have property:

public virtual IList<Base> ListofAandB {get;set;}

If I remove BaseMap definition and just map A and B as ClassMap<> I receive error "Cannot find map definition for Base" if I try to use the property that is written above.

Was it helpful?

Solution

You won't be able to map your ListOfAAndB property if your inheritance is present only in your code and not in your database model. To have NHibernate map this property as it is defined, you are going to need a Base table in your database.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top