Part 16 Self referencing association in entity framework04:33

  • 0
Published on November 22, 2017

Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video

In this video, we will discuss self referencing association in entity framework with database first approach. Let us understand self-referencing association with an example.

We will be using Employees table in this demo. This table is a self-referencing table because to get the manager of an employee, we take ManagerId column value and look up in the EmployeeId column of the same table.

Now if you generate an ADO.NET entity data model based on this Employees table, Employee entity is generated. Notice that a self-referencing association and 2 navigation properties (Employees1 & Employee1) are automatically created.

Right click on Employees1 navigation property and select properties. In the properties window notice that the Multiplicity is set to Many. So, this navigation property returns employees who are subordinates.

Similarly, right click on Employee1 navigation property and select properties. In the properties window notice that the Multiplicity is set to Zero or One. So, this navigation property returns the manager of an employee.

From a developer perspective these navigation property names are difficult to understand. If you have to write any code based on these navigation properties, it can get even more complicated. For example, let us say we want to display Employee names and their respective manager names.

To achieve this we would drag and drop a GridView control on the webform and write the following code in the code-behind file Page_Load event. Notice that, because of the poor naming of the navigation properties the code is hard to read and maintain.
protected void Page_Load(object sender, EventArgs e)
{
EmployeeDBContext employeeDBContext = new EmployeeDBContext();
GridView1.DataSource = employeeDBContext.Employees.Select(emp =] new
{
EmployeeName = emp.EmployeeName,
ManagerName = emp.Employee1 == null ? “Super Boss” : emp.Employee1.EmployeeName
}).ToList();
GridView1.DataBind();
}

Now let’s give these navigation properties meaningful names. To do this,
1. Right click on Employees1 navigation property and rename it to Subordinates
2. Similarly, right click on Employee1 navigation property and rename it to Manager

Now the code in the code-behind file would change as shown below which is more readable and maintainable.
protected void Page_Load(object sender, EventArgs e)
{
EmployeeDBContext employeeDBContext = new EmployeeDBContext();
GridView1.DataSource = employeeDBContext.Employees.Select(emp =] new
{
EmployeeName = emp.EmployeeName,
ManagerName = emp.Manager == null ? “Super Boss” : emp.Manager.EmployeeName
}).ToList();
GridView1.DataBind();
}

https://cafeadobro.ro/

https://www.stagebox.uk/wp-includes/depo10-bonus10/

depo 25 bonus 25

https://parfumschristianblanc.com/

https://www.barplate.com/wp-includes/js/qris/

https://hotmusic507.org/

Enjoyed this video?
"No Thanks. Please Close This Box!"