views:

128

answers:

2

Is it possible to auto map a simple nested object structure?

Something like this:

public class Employee : Entity
{
    public Employee() { 
         this.Manages = new List<Employee>();
    }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual bool IsLineManager { get; set; }
    public virtual Employee Manager { get; set; }
    public virtual IList<Employee> Manages { get; set; }
}

It causes the following error at run time:

Repeated column in mapping for collection: SharpKtulu.Core.Employee.Manages column: EmployeeFk

Is it possible to automap this sort of structure, or do I have override the auto mapper for this sort of structure?

+1  A: 

This is probably because your conventions creates foreign keys as "class name" + "Fk". So you get same FK column for both Manager and Manages properties. You can override conventions so that FK column includes property name and thus you''ll get EmployeeManagerFk and EmployeeManagesFk. Or include both left/right side classes, too (EmployeeManagerInEmployeeFk), etc.

See here on how to override conventions. Or you can override HasMany/ManyToMany conventions to have different setup. Read FNH docs, look in google, read sample code - conventions are not always easy to understand and get to work.

Another very useful option is to export .hbm files. NHibernate error messages are not always user-friendly, but I usually find errors by looking at exported .hbm files - e.g. you can compare them before/after your recent "breaking" change to see what's happening. In your case you would easily see where are the duplicating names applied.

queen3

related questions