To complete this task, I am going to create Employee and Department Model Class and let’s see how to make relationship. Following are the two model class.
- public class Department
- {
- [Key]
- public int DepartmentId { get; set; }
- [Required]
- public string DepartmentName { get; set; }
- }
- public class Employee
- {
- [Key]
- public int EmployeeId { get; set; }
- [Required]
- public string EmployeeName { get; set; }
- }
- public class Department
- {
- [Key]
- public int DepartmentId { get; set; }
- [Required]
- public string DepartmentName { get; set; }
- }
- public class Employee
- {
- [Key]
- public int EmployeeId { get; set; }
- [Required]
- public string EmployeeName { get; set; }
- // Foreign key
- [Display(Name = "Department")]
- public virtual int DepartmentId { get; set; }
- [ForeignKey("DepartmentId")]
- public virtual Department Departments { get; set; }
- }
- [ForeignKey("DepartmentId")]
- public virtual Department Departments { get; set; }
- [Display(Name = "Department")]
- public virtual int DepartmentId { get; set; }

Santhakumar MunuswamyPosted Nov 16, 2015, 11:52 PM
Nice share