Leroy
Leroy

Reputation: 318

Fluent NHibernate trying to map subclass of subclass using table-per-subclass

I am building an application with heavy inheritance and have a part where there exists classes, A, B and C with:

class A

class B : A

class C : B

I implemented subclass mapping as a table-per-subclass style for class B as follows:

class BMap : SubclassMap<B>
{
    public BMap()
    {
        Extends<A>();
        KeyColumn("ID");
    }
}

Which works perfectly. However, when I want to implement C as follows:

class CMap : SubclassMap<C>
{
    public CMap()
    {
        Extends<B>();
        KeyColumn("ID");
    }
}

It results in the error

Duplicate class/entity mapping

I browsed the Hibernate/NHibernate forum but could not find an answer to this problem.

Upvotes: 4

Views: 1329

Answers (1)

Firo
Firo

Reputation: 30813

this does work as expected with NH 3.3.1.4000

public class A
{
    public virtual int Id { get; protected set; }
}

public class B : A { }
public class C : B { }

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

public class BMap : SubclassMap<B> { }
public class CMap : SubclassMap<C> { }
public static void Main(string[] args)
{
    var config = Fluently.Configure()
        .Database(SQLiteConfiguration.Standard.InMemory().ShowSql().FormatSql())
        .Mappings(m => m.FluentMappings
            .Add<AMap>()
            .Add<BMap>()
            .Add<CMap>()
        )
        .BuildConfiguration();

    using (var sf = config.BuildSessionFactory())
    using (var session = sf.OpenSession())
    {
        new SchemaExport(config).Execute(true, true, false, session.Connection, null);
        session.Save(new C());
        session.Flush();
    }
}

Upvotes: 1

Related Questions