In this Video, I am going to discuss Working with Multiple tables in ASP.NET MVC applications using Entity Framework. Please read our previous Video where we discussed Action Link HTML Helper in ASP.NET MVC application. At the end of this Video, you will understand how to work with multiple tables in ASP.NET MVC Application using Entity Framework.
How to Work with Multiple Tables in ASP.NET MVC using Entity Framework?. The Complete ASP.NET MVC Developer Course 2023 [Videos].
Database tables used in this demo:
We are going to work with the below two tables i.e. Department and Employee.
Please use the below SQL script to create and populate Department and Employee tables with the required test data.
Example to understand working with multiple tables in MVC Application:
Following is our business requirement.
- We need to display all the departments from the Department table. The Department names should be rendered as hyperlinks. This is going to be our department list page.
- On clicking the department name link, all the employees in that particular department should be displayed. The employee names also here going to be rendered as hyperlinks. This is going to be our employee list page.
- When the user clicks on the employee name link the full details of that employee should be displayed in the browser. This is going to be our employee details page.
- A link should also be provided on the employee full details page to navigate back to the Employee List page. Along the same lines, a link should also be provided on the employee list page to navigate back to the Departmentss list page.
The following image gives you the overall workflow of our requirement
Note: We are going to work with the same example that we started in our previous two Videos. So please read the below two Videos before proceeding to this Video.
Entity Framework in ASP.NET MVC
Update the EDMX file
Double click on the EmployeeDataModel.edmx file which is in the Models folder. Once you click on the edmx file the following screen will open.
Right-click anywhere in the edmx file and then click on the “update model from the database” option as shown in the below image.
Then choose the add button and then select the Department table as shown in the below image.
Next, choose the Refresh button and select the Employee table and click on the Finish button as shown in the below image.
Once you click on the Finish button, you will get an error saying Salary Property is not mapped. Simply select the Salary Property from the Employee Model of the edmx file, right-click on it, and then click on delete from the model as shown in the below image.
Thats it. Save the edmx file and build the solution. Lets have a look of the files that are generated and modified by Entity Framework.
Department.cs (This file is added by Entity Framework)
Employee.cs (This file is modified by Entity Framework)
EmployeeDataModel.Context.cs (This file is modified by Entity framework)
These are the changes done by entity framework.
Add Department Controller
Right click on the “Controllers” folder and add a MVC5 Empty Controller with the name DepartmentController and then Copy and paste the following code into it.
Adding Index View Of Department Controller:
Right-click on the Index() action method in DepartmentController class and select “Add View” from the context menu. Set all the default values as it is. Copy and paste the following code in Index.cshtml view file in Department folder.
In the above code, we are using the following ActionLink HTML helper extension which takes five parameters.
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes);
Modify Employee Controller:
Add “departmentId” parameter to Index() action method in “EmployeeController” class. Use the “departmentId” parameter to filter the list of employees. After changes Employee Controller looks as shown below.
Copy and paste the following code in “Index.cshtml” that is present in the “Employee” folder in the “Views” folder. With this change, we are able to generate an action link to redirect the user to a different controller action method.
Modify the Details.cshtml file that is present in Employee Folder.
Here, we are just removing the Salary property.
Change the RouteConfig file as shown below where we provide the default Route as Index Action Method of Department Controller.
Thats it we have done with our implementation. Now run the application and see everything is working as expected or not.