Fluent Nhibernate Mapping Single class on two database tables

Posted by nabeelfarid on Stack Overflow See other posts from Stack Overflow or by nabeelfarid
Published on 2010-06-12T13:39:59Z Indexed on 2010/06/12 13:42 UTC
Read the original article Hit count: 326

Hi guys,

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

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about fluent-nhibernate