Search code examples
nhibernatefluent-nhibernateautomapping

Fluent Nhibernate Mapping Single class on two database tables


I am having problems with Mapping.

I have two tables in my database as follows: Employee and EmployeeManagers

Employee

EmployeeId int Name nvarchar

EmployeeManagers

EmployeeIdFk int ManagerIdFk int

So the employee can have 0 or more Managers. A manager itself is also an Employee.

I have the following class to represent the Employee and Managers

public class Employee
{
public virtual int Id
{
    get;
    set;
}

public virtual string Name
{
    get;
    set;
}

public virtual IList<Employee> Managers
{
    get;
    protected set;
}

public Employee()
{
    Managers = new List<Employee>();
}
}

I don't have any class to represent Manager because I think there is no need for it, as Manager itself is an Employee.

I am using autoMapping and I just can't figure out how to map this class to these two tables. I am implementing IAutoMappingOverride for overriding automappings for Employee but I am not sure what to do in it.

public class NodeMap : IAutoMappingOverride
{
    public void Override(AutoMapping<Node> mapping)
    {
        //mapping.HasMany(x => x.ValidParents).Cascade.All().Table("EmployeeManager");
        //mapping.HasManyToMany(x => x.ValidParents).Cascade.All().Table("EmployeeManager");
    }
}

I also want to make sure that an employee can not be assigned the same manager twice. This is something I can verify in my application but I would like to put constraint on the EmployeeManager table (e.g. a composite key) so a same manager can not be assigned to an employee more than once.

Could anyone out there help me with this please?

Awaiting Nabeel


Solution

  • I did something like this, maybe it could help you start out?

    http://www.dbones.co.uk/blog/post/2010/04/Nhib-Self-Reference-Object.aspx

    Edit opps i can see its multiple managers

    Bones