Automapping inheritance with Fluent nhibernate
Asked Answered
A

1

6

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?

Antitank answered 11/4, 2013 at 8:12 Comment(0)
A
2

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...

Antitank answered 15/4, 2013 at 12:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.