entity framework navigational property in model classes

arsar 121 Reputation points
2021-07-29T07:39:08.163+00:00

I was learning relationships in model classes. Please see the attached image for the table relationships.
There are three tables, department, employee, location.
When model classes are created for these tables, I am confused regarding navigational property.
employee class:

public class employee  
{  
 public employee()  
 {  
 this.location = new HashSet<location>();  
 }  
 //attributes  
  
 public virutal department department {get;set}  
 public virtual ICollection<location> locations {get;set}  
}  

then in department class:

public class department  
{  
 //attributes  
 public virutal ICollection<employee> employees {get;set}  
}  

in location class:

public class location  
{  
 public virutal employee employee {get;set}  
}  

Why in employee class department is defined like virutal department department but location is defined as virtual ICollection<location> locations. Why using ICollection only with locataion?
And in department model, employee class is defined as virutal ICollection<employee> employees but in location model employee is defined as virutal employee employee. Why is it so, please clarify.
Also in employee class location is defined as HashSet<location>() in constructor, and why is it defined like this ?

118904-table-design.png

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,367 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,252 questions
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,611 Reputation points
    2021-07-29T09:24:16.81+00:00

    Hi arsar-2618,
    First you need to understand two attributes:
    >> ICollection<location> locations {get;set}; ICollection<employee> employees {get;set}
    Collection navigation property: A navigation property that contains references to many related entities.
    >> virutal employee employee {get;set};virutal department department {get;set}
    Reference navigation property: A navigation property that holds a reference to a single related entity.
    The relationship between the three tables is that employees can only belong to one department but can have multiple positions. A department can have multiple employees, and a location can only have one employee
    The definition of the location class allows at most one employee through the employee navigation attribute (refer to the multiplicity of the navigation attribute is 0 or 1), and the definition of the employee class allows each employee to have multiple locations through the locations navigation attribute (with multiple sets of many Navigation attributes) and department navigation attributes belong to only one department.
    The main entity in a one-to-many relationship is an entity with set navigation attributes, and a dependent entity is an entity with reference navigation attributes.
    More details please refer to this document.
    And about HashSet<location>(), I suggest you refer to this thread which explains it in detail.
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful