none
how to map many to many relationship with additional fields ? RRS feed

  • Question

  •  hi ,

    I have three tables  lets say ,  employee , department and employeeDepartment. Moreover, an  employee has many department and a department has employees. and  it is obvious i have to add a another relation which is employeeDepartment. And apart from the ID s of these tables i have from/ to date in the relationship table (extra 2 fields). My question is how do I map this with the entity framework , and do i have to tell explicitly about the relationship using fluent APIs ?

    currently, i have

          

     public class Employee
        {
            public int ID { get; set; }

           public virtual ICollection<DeptEmployee> DeptEmployees { get; set; }
        }

     public class Department
        {     

            public string ID { get; set; }
            public string Name { get; set; }
            public virtual ICollection<DeptEmployee> DeptEmployees { get; set; }
       
        }

     public class DeptEmployee
        {
            public string DepartmentNo { get; set; }
            public int EmployeeID { get; set; }
            public DateTime From { get; set; }
            public DateTime To { get; set; }
            public virtual Employee Employee { get; set; }
            public virtual Department Department { get; set; }
        }

     So ,  Again my question is that , whether this is correct and  is it necessary to declare the relationship using fluent apis , if so how ?

    Thank you



    Monday, December 3, 2012 10:50 AM

Answers

  • Hi Roshan605,

    Welcome to the MSDN forum.

    Please check whether this is what you want:

        public class Employee
        {
            public int ID { get; set; }
            public virtual ICollection<DeptEmployee> DeptEmployees { get; set; }
        }
    
        public class Department
        {
            public string ID { get; set; }
            public string Name { get; set; }
            public virtual ICollection<DeptEmployee> DeptEmployees { get; set; }
        }
    
        public class DeptEmployee
        {
            public string DepartmentID { get; set; }
            public virtual Department Department { get; set; }
            public int EmployeeID { get; set; }
            public virtual Employee Employee { get; set; }
            public DateTime From { get; set; }
            public DateTime To { get; set; }
        }
    
        public class tstContext : DbContext
        {
            public DbSet<Employee> Mensajes { get; set; }
            public DbSet<DeptEmployee> Mercados { get; set; }
            public DbSet<Department> Trazas { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<DeptEmployee>().HasKey(p => new { p.EmployeeID, p.DepartmentID });
                modelBuilder.Entity<Employee>().HasMany(p => p.DeptEmployees).WithRequired().HasForeignKey(p => p.EmployeeID);
                modelBuilder.Entity<Department>().HasMany(p => p.DeptEmployees).WithRequired().HasForeignKey(p => p.DepartmentID);
            }
        }
    

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Alexander Sun Wednesday, December 26, 2012 2:03 AM
    Wednesday, December 5, 2012 3:12 AM