R – Fluent NHibernate table per class hierarchy multiple tables mapping problem

fluent-nhibernatenhibernatenhibernate-mapping

I've got a problem with fluent nhibernate table per class hierarchy mapping. I've got 2 domain objects, container (baseclass) and album (subclass). Album only contains a constructor. Container dervies from EntityWithTypedId from Sharp Architect. EntityWithTypedId provides the key of type Guid (the name is ContainerId).

public class Container : EntityWithTypedId<Guid>
{
    public Container()
    {
    }

    protected Container(Guid userId)
        : this()
    {
        UserId = userId;
    }

    public virtual int Type { get; set; }

    public virtual Guid UserId { get; set; }
}

public class Album : Container
{
    public Album()
        : base()
    {
        Type = (int)ContainerType.Album;
    }

    public Album(Guid userId)
        : base(userId)
    {
        Type = (int)ContainerType.Album;
    }
}

I want all the domain objects to be saved in a single table called "Containers". I've got a mapping file for Container:

public class ContainerMap : IAutoMappingOverride<Container>
{
    public void Override(AutoMap<Container> mapping)
    {
        mapping.DiscriminateSubClassesOnColumn<int>("Type");
    }
}

NHibernate assumes 2 tables are used. The table "Containers" is mapped as expected, but NHibernate assumes theres another table "Album" only containing an Id called "Container" which is equal to ContainerId in table "Containers". How can i change the mapping so the table "Album" isn't needed?

If i provide a mapping class for Album i get a mapping error even if the album mapping is empty:
FluentNHibernate.Cfg.FluentConfigurationException : An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

  • Database was not configured through Database method.

    —-> System.ArgumentException : Object of type 'FluentNHibernate.AutoMap.AutoMap1[Core.Album]' cannot be converted to type 'FluentNHibernate.AutoMap.AutoMap1[Core.Container]'.

Thanks!

/Marcus

Best Answer

Don't handle Type as a property, its handled automatically.

Related Topic