Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automapping inheritance with Fluent nhibernate

I have this situation:

public namespace ANamespace
{
    public abstract class ABase:IABase
    {
        //properties
    }

    public abstract class A : ABase
    {
        //properties
    }

    public class A1 : A
    {
        //properties
    }

    public class A2 : A
    {
        //properties
    }
}

If I use this mapping code:

AutoMap
   .AssemblyOf<ABase>()
   .Where(e => e.Namespace == "ANamespace")
   .IncludeBase<A>().IgnoreBase<ABase>();

only A table is created (with ABase and A properties). If I delete IncludeBase() then A1 and A2 are created (with all properties).

AutoMap
   .AssemblyOf<ABase>()
   .Where(e => e.Namespace == "ANamespace")
   .IgnoreBase<ABase>();

How to write mapping to have tables for classes A (with all A and ABase properties), A1 and A2 (with specific properties) in my database but not for the class ABase?

like image 580
Athina Avatar asked Apr 11 '13 08:04

Athina


1 Answers

After three days I finally found solution to this problem. It's not enough to have IncludeBase<T>(). You have to map base class too. So the solution is:

AutoMap
  .AssemblyOf<ABase>()
  .Where(type=>type.IsSubclassOf(typeof(A)) || type==typeof(A))
  .IncludeBase<A>();

I hope it will help some similar problems...

like image 104
Athina Avatar answered Sep 21 '22 16:09

Athina