C# – Fluent nhibernate automap subclasses from different assemblies

automappingcfluent-nhibernatemappingnhibernate

What I need to do is automap subclasses of my abstract page class. I need to find them in a list of assemblies that I get a runtime (at the initialization stage). I don't have any reference to the assemblies from the assembly of my mappings.

My page class looks something like this:

public abstract class Page : EntityBase
{
    public virtual int Id { get; protected set; }
    public virtual Page Page { get; set; }
    //Other properties
}

My mappings looks like this:

var persistenceModel = new AutoPersistenceModel();
_assemblies.ForEach(x => persistenceModel.AddEntityAssembly(x));
persistenceModel.Setup(x =>
        {
            x.SubclassStrategy = y => SubclassStrategy.Subclass;
            x.DiscriminatorColumn = y => "Type";
        })
.IncludeBase(typeof(Page))
.OverrideAll(x => x.IgnoreProperties(y => typeof(Page)
.IsAssignableFrom(y.ReflectedType) && y.ReflectedType != typeof(Page)))
.Where(y => typeof(EntityBase).IsAssignableFrom(y));

_assemblies is a list of assemblies to search for mappable entities in. It contains the assembly of my page class as well as one or more other assemblies that all have a reference to the base assembly (the one with my page class).

When I add a class that inherits from my page class to the same assembly as my page class all works fine and its mapped appropriately. But if I add one to one of the other assemblies that are loaded at runtime I get problems. I get this error:

"Tried to add many-to-one 'Page' when already added."

It finds the class but for some reason tries to map it as Page or something. But the page class is already mapped so I get that error. That is my guess. Does anyone know if I can work around this in any way? Or know a solution?

Best Answer

I just found this link. Seems like this isn't possible just yet.

Edit

This is now fixed with the last release of fluent nhibernate.