Mapping issue

Original Post nabeelfarid's Avatar

nabeelfarid

10 Jun, 2010 03:42 PM via web

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

  1. 2 Posted by nabeelfarid on 14 Jun, 2010 08:36 AM

    nabeelfarid's Avatar

    Could anyone please help me with this issue?

    Awaiting
    Nabeel

  2. 3 Posted by PrzemasG on 14 Jun, 2010 09:40 PM

    PrzemasG's Avatar

    I think you should use two lists:

     public class Employee
     {
        public virtual IList<Employee> Managers
        public virtual IList<Employee> Employees
     }
    

    and mappingOverride should look like this:

     public class EmployeeMappingOverride : IAutoMappingOverride<Employee>
     {
       public void Override(AutoMapping<Employee> mapping)
       {
           mapping.HasManyToMany(x => x.Managers).Cascade.All().Table("EmployeeManager");
           mapping.HasManyToMany(x => x.Employees).Cascade.All().Inverse().Table("EmployeeManager");
       }
     }
    

    And maybe you will have to use:

                 ChildKeyColumn("name1")
                .ParentKeyColumn("name2")
                .ForeignKeyConstraintNames("name3","name4")
    

Reply to this discussion

Preview Comments are parsed with Markdown. Help with syntax

Attached Files

    You can attach files up to 10MB

    What is 14 minus 4?

    Recent Discussions

    05 Jul, 2010 10:29 PM
    05 Jul, 2010 12:45 PM
    05 Jul, 2010 12:42 PM
    05 Jul, 2010 12:17 PM
    05 Jul, 2010 12:12 PM

     

    03 Jul, 2010 12:26 AM
    02 Jul, 2010 02:17 PM
    02 Jul, 2010 08:18 AM
    02 Jul, 2010 12:20 AM
    01 Jul, 2010 10:14 PM