André Rodrigues
André Rodrigues

Reputation: 131

Fluent hierarchy

I have the following situation with fluent nhibernate:

public class Stuff
{
    public Stuff()
    {
        Entities = new List<Entity>();
    }

    public virtual int Id { get; set; }
    public virtual IList<Entity> Entities { get; set; }
}

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

    public virtual string Type { get; set; }
    public virtual Stuff Stuff { get; set; }
}

public class Person : Entity
{
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
}

public class Animal : Entity
{
    public virtual string Species { get; set; }
}

And then, i have the following code to use automap and generate these mappings:

        var sessionFactory =
            Fluently.Configure().Database(persistenceConfigurer).Mappings(
                m =>
                m.AutoMappings.Add(
                    AutoMap.Source(new Types(typeof(Entity), typeof(Person), typeof(Animal), typeof(Stuff))))
                    .ExportTo(@"e:\")).ExposeConfiguration(BuildSchema).BuildSessionFactory();

however, what's happening is that i get the following exception:

---> NHibernate.MappingException: Association references unmapped class: ConsoleApplication1.Models.Entity

if i make the entity class non abstract this works, however, i'd like to avoid having that table in the database but still maintain the hierarchy concept with the re

Upvotes: 2

Views: 172

Answers (1)

Mike
Mike

Reputation: 4287

You need to add your auto mappings like this

AutoMap.AssemblyOf<Entity>(yourConfiguration).IgnoreBase<Entity>();

Not only will this ignore your Entity base class but you don't need to add each entity manually so long as each model inherits from Entity.

Upvotes: 2

Related Questions