0
votes

I am using Fluent NHibernate and table per concrete class for inheritance mappings.

There is an abstract base class and two other subclasses.

My base class has Id column. Created tables are ok. All tables has its own Id column. But sequence is only one for these two tables.

I want to assign different sequence for every subclass.

public abstract class Base
{
    public virtual int Id { get; set; }
}

public class BaseMap : ClassMap<Base>
{
    public BaseMap()
    {
        Id(x => x.Id).GeneratedBy.Native();
    }
}

public class A : Base
{
    public virtual int AmountOfA { get; set; }
}

public class AMap : ClassMap<A>
{
    public AMap()
    {
        Map(x => x.AmountOfA );
    }
}

public class B : Base
{
    public virtual int AmountOfB { get; set; }
}

public class BMap : ClassMap<B>
{
    public BMap()
    {
        Map(x => x.AmountOfB );
    }
}

Is this possible with Fluent NHibernate?

1
What database are you using? - Martin Ernst

1 Answers

0
votes

this is by design.

  • session.Get<Base>(1); would have undefined behavior because the id is not unique

  • consider the case when there is a reference to the base class Reference(x => x.SomeBase); with a the value 1 in the database: it would not be possible to know if A(Id: 1) or B(Id: 1) is the object referenced

if Base is only there to reuse the Id property then dont map it as own entity but create a base class

public class BaseMap<T> : ClassMap<T> where T : Base
{
    public BaseMap()
    {
        Id(x => x.Id).GeneratedBy.Native();
    }
}

public class AMap : BaseMap<A>
{
    public AMap()
    {
        Map(x => x.AmountOfA );
    }
}