简体   繁体   中英

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.

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM