views:

36

answers:

2

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

A: 

Hi

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

dbones
Automapping in the new fluent nhibernate version 1.1 now supports self referecing.
nabeelfarid
Epic! thanks for the update
dbones
A: 

I end up doing like this

    public abstract class Node
    {
        public virtual int Id
        {
            get;
            set;
        }

        public virtual Node ParentNode
        {
            get;
            set;
        }

        public virtual IList<Node> ChildNodes
        {
            get;
            protected set;
        }

        protected Node()
        {
            ChildNodes = new List<Node>();
        }

        public virtual void AddChildNode( Node childNode )
        {
            childNode.ParentNode = this;
            ChildNodes.Add( childNode );
        }

    }

 public class NodeMap : IAutoMappingOverride<Node>
{
    public void Override( AutoMapping<Node> mapping )
    {
        //self referencing
        //http://stackoverflow.com/questions/1547956/fluent-nhibernate-automappings-with-self-reference
        mapping.References( x => x.ParentNode ).Column( "ParentNodeFk" ).Cascade.SaveUpdate();
        mapping.HasMany( x => x.ChildNodes ).Cascade.SaveUpdate().KeyColumn( "ParentNodeFk" );            

    }
}
nabeelfarid
I also have to use the latest version of Fluent NHibernate i.e. 1.1 where automappign now supports Self Referencing.This http://stackoverflow.com/questions/1547956/fluent-nhibernate-automappings-with-self-reference also helped me
nabeelfarid