NHibernate SubclassMap gives DuplicateMappingException

Posted by stiank81 on Stack Overflow See other posts from Stack Overflow or by stiank81
Published on 2010-06-15T12:52:02Z Indexed on 2010/06/15 20:02 UTC
Read the original article Hit count: 1033

I'm using NHibernate to handle my database - with Fluent configuration. I'm not using Automappings. All mappings are written explicitly, and everything is working just fine.

Now I wanted to add my first mapping to a subclass, using the SubclassMap, and I run into problems. With the simplest possible setup an Nhibernate DuplicateMappingException is thrown, saying that the subclass is mapped more than once:

NHibernate.MappingException : Could not compile the mapping document: (XmlDocument) ----> NHibernate.DuplicateMappingException : Duplicate class/entity mapping MyNamespace.SubPerson

I get this with my simple classes written for testing:

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class SubPerson : Person
{
    public string Foo { get; set; }
}

With the following mappings:

public class PersonMapping : ClassMap<Person>
{
    public PersonMapping()
    {
        Not.LazyLoad();
        Id(c => c.Id); 
        Map(c => c.Name); 
    }
}

public class SubPersonMapping : SubclassMap<SubPerson>
{
    public SubPersonMapping()
    {
        Not.LazyLoad();
        Map(m => m.Foo); 
    }
}

Any idea why this is happening? If there were automappings involved I guess it might have been caused by the automappings adding a mapping too, but there should be no automapping. I create my database specifying a fluent mapping:

private static ISession CreateSession()
{
    var cfg = Fluently.Configure().
            Database(SQLiteConfiguration.Standard.ShowSql().UsingFile("unit_test.db")).
            Mappings(m => m.FluentMappings.AddFromAssemblyOf<SomeClassInTheAssemblyContainingAllMappings>());
    var sessionSource = new SessionSource(cfg.BuildConfiguration().Properties, new TestModel());
    var session = sessionSource.CreateSession();
    _sessionSource.BuildSchema(session);
    return session;
}        

Again; note that this only happens with SubclassMap. ClassMap's are working just fine!

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about fluent-nhibernate