Free NHibernate Automapping Bidirectional Relationships

I am trying to automate my domain model using free nhibernate. In this particular case, I have a one-to-many bidirectional relationship that I need to map. The problem is that he does not automatically perceive this as a bi-directional relationship, but as two different relations and creates a separate foreign key for each.

How can I say this is the same attitude? I hope I'm clear enough.

+3
source share
1 answer

You can override the machine using 1.0RC. Try this example from the SharpArchitecture bidirectional mapping from Employee to Territory, where Territory is a relationship inverse:

   public class EmployeeMap : IAutoMappingOverride<Employee>
     {
        public void Override(AutoMap<Employee> mapping) {

        //... other omitted mappings...

        mapping.HasManyToMany<Territory>(x => x.Territories)
            .WithTableName("EmployeeTerritories")
            .WithParentKeyColumn("EmployeeID")
            .WithChildKeyColumn("TerritoryID")
            .AsBag();
    }
}

   public class TerritoryMap : IAutoMappingOverride<Territory>
{
    public void Override(AutoMap<Territory> mapping) {

        //... other omitted mappings...

        mapping.HasManyToMany<Employee>(x => x.Employees)
            .WithTableName("EmployeeTerritories")
            .Inverse()
            .WithParentKeyColumn("TerritoryID")
            .WithChildKeyColumn("EmployeeID")
            .AsBag();
    }
}
+3
source

Source: https://habr.com/ru/post/1716780/


All Articles