Marcus
Marcus

Reputation: 2480

Fluent NHibernate table per class hierarchy multiple tables mapping problem

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.

Thanks!

/Marcus

Upvotes: 1

Views: 1261

Answers (1)

Marcus
Marcus

Reputation: 2480

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

Upvotes: 1

Related Questions