Question

I'm working with FluentNhibernate. I'm trying to make my Map files are light weight as possible by relying on Convention files for things like table names, foreign key columns etc.

I'm having a bit of a problem with my HasManyToManyConvention.

I have 2 object. Models.Processor and Models.Worker.

A processor can be associated with Many workers, and vice versa.

public class Processor : Entity
{
    ...
    public virtual IList<Worker> Workers { get; set; }
    public virtual void AddWorker(Worker worker)
    {
        worker.AddProcessor(this);
        Workers.Add(worker);
    }
}

public class Worker : Entity
{
    ...
    public virtual IList<Processor> Processors { get; set; }
    public virtual void AddProcessor(Processor processor)
    {
        processor.AddWorker(this);
        Processors.Add(processor);
    }
}

I also have 2 maps which map the 2 Entities to one another.

public ProcessorMap()
{
    HasManyToMany<Processor>(x => x.Workers)
            .Table("WorkerProcessor")
            .ParentKeyColumn("ProcessorId")
            .ChildKeyColumn("WorkerId");
}

At this point everything works fine.

And the reciprocal setup for the WorkerMap with the parent & child key's reversed. I'd like to set it up in such a way that I don't need to specify the table & parentkey/childkey properties in the map so I've added some conventions to my project and include them in the fluent configuration.

public class ForeignKeyConvention : FluentNHibernate.Conventions.ForeignKeyConvention
{
    protected override string GetKeyName(Member property, Type type)
    {
        if (property == null)
            return type.Name + "Id";

        return property.Name + "Id";
    }
}

public class HasManyToManyConvention : IHasManyToManyConvention
{
    public void Apply(IManyToManyCollectionInstance instance)
    {
        instance.Cascade.SaveUpdate();
    }
}

public class ManyToManyTableNameConvention :
             FluentNHibernate.Conventions.ManyToManyTableNameConvention
{
    protected override string GetBiDirectionalTableName(
              IManyToManyCollectionInspector collection,
              IManyToManyCollectionInspector otherSide)
    {
        return collection.EntityType.Name + otherSide.EntityType.Name;
    }

    protected override string GetUniDirectionalTableName(
              IManyToManyCollectionInspector collection)
    {
        return collection.EntityType.Name + collection.ChildType.Name);
    }
}

So what happens when I debug this, is that it steps into the method GetBiDirectionalTableName and for some reason, both the collection and otherside properties are both of type Processor. I would have expected 1 to be Processor and one to be Worker. The upshot is that FN throws the following exception.

NHibernate.MappingException : Repeated column in mapping for collection:
    Core.Models.Processor.Workers column: ProcessorId

Any help/pointers would be appreciated.

Was it helpful?

Solution

Turns out I was missing some extra params on the HasManyToMany() mappings.

You need to specify that one of them is Inverse()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top