Getting Started With ASP.NET Core And Entity Framework Core

Entity Framework is an Object-Relational Mapper (ORM) that allows developers to work with a database using .NET objects. It eliminates the need for most of the data-access code that developers usually need to write. .NET Core comes with a new version of Entity Framework that is Entity Framework Core.

EF core is light weight, cross-platform version of Entity Framework. Most of the top-level APIs remain the same in it, so developers coming from Entity Framework 6.x feel comfortable working with it but there are many features that are part of EF 6.x but not of EF core. For example - complex/value types, many-to-many relationship without join entity, last loading  etc.
 
Some new features are introduced in EF Core which were missing in EF 66.x, like shadow state property,  table per type (TPT), alternates keys. In this article, we will learn how to create an ASP.NET Core MVC application using Entity Framework Core 1.0.  We will create an EmployeeDetail project, in which we will display the list of all employees, details of any specific employee, and update or delete records of an employee. Later, we will also add the features of sorting and searching operation.

Pre Requests

Before staring the project, we must have the following configuration.

  • Visual Studio 2015 Update 3
  • .NET Core
  • SQL Server Management Studio

If you already have all this configuration, then it is great; otherwise install all these prerequisites first.

Create an ASP.NET Core Application

Open Visual Studio and create a new ASP.NET Core project named as “EmployeeDetail”.


From templates section, select the “Web Application” template for the project and click on “Ok” button.


After successful creation of the project, you will get the following project structure.


Now, press “F5” and run the application. When we run this application, we will get the following output.


This is the default screen that we get in our application. But, we don’t need this default screen, so remove unnecessary code from “_layout” and “Index” views. 

Now, go to “ _Layout” partial View and replace the content of page with the below code. 

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <meta charset="utf-8" />  
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0" />  
  6.     <title>@ViewData["Title"] - EmployeeDetail</title>  
  7.   
  8.     <environment names="Development">  
  9.         <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />  
  10.         <link rel="stylesheet" href="~/css/site.css" />  
  11.     </environment>  
  12.     <environment names="Staging,Production">  
  13.         <link rel="stylesheet" href="https//ajax.aspnetcdn.com/ajax/bootstrap/3.3.6/css/bootstrap.min.css"  
  14.               asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"  
  15.               asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />  
  16.         <link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />  
  17.     </environment>  
  18.     @Html.ApplicationInsightsJavaScript(TelemetryConfiguration)  
  19. </head>  
  20. <body>  
  21.     <div class="navbar navbar-inverse navbar-fixed-top">  
  22.         <div class="container">  
  23.             <div class="navbar-header">  
  24.                   
  25.                 <a asp-area="" asp-controller="Home" asp-action="Index" class="navbar-brand">EmployeeDetail</a>  
  26.             </div>  
  27.             
  28.         </div>  
  29.     </div>  
  30.     <div class="container body-content">  
  31.         @RenderBody()  
  32.         <hr />  
  33.         <footer>  
  34.             <p>© 2017 - EmployeeDetail</p>  
  35.         </footer>  
  36.     </div>  
  37.   
  38.     <environment names="Development">  
  39.         <script src="~/lib/jquery/dist/jquery.js"></script>  
  40.         <script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>  
  41.         <script src="~/js/site.js" asp-append-version="true"></script>  
  42.     </environment>  
  43.     <environment names="Staging,Production">  
  44.         <script src="https//ajax.aspnetcdn.com/ajax/jquery/jquery-2.2.0.min.js"  
  45.                 asp-fallback-src="~/lib/jquery/dist/jquery.min.js"  
  46.                 asp-fallback-test="window.jQuery">  
  47.         </script>  
  48.         <script src="https//ajax.aspnetcdn.com/ajax/bootstrap/3.3.6/bootstrap.min.js"  
  49.                 asp-fallback-src="~/lib/bootstrap/dist/js/bootstrap.min.js"  
  50.                 asp-fallback-test="window.jQuery && window.jQuery.fn && window.jQuery.fn.modal">  
  51.         </script>  
  52.         <script src="~/js/site.min.js" asp-append-version="true"></script>  
  53.     </environment>  
  54.   
  55.     @RenderSection("scripts", required false)  
  56. </body>  
  57. </html>   

And also, replace the content of “Index” View with the following code.

  1. @{  
  2.     ViewData["Title"] = "Home Page";  
  3.   
  4.   
  5. <div class="row">  
  6.    <div class="col-md-12">  
  7.        <h3>Employee Application</h3>  
  8.    </div>  
  9. </div>   

After all the above changes, now our application will look like below.


Add Entity Framework Core Dependency

In our application, we will use SQL Server for the database, so we need to install the Entity Framework for SQL Server. To use SQL Server, the package is “Microsoft.EntityFrameworkCore.SqlServer”. Now, we need to add the configuration of Entity Framework in project.

Open your Project.json file and add the following lines under dependency section.

  1. "Microsoft.EntityFrameworkCore.SqlServer" "1.0.0",  
  2.     "Microsoft.EntityFrameworkCore.Tools" {  
  3.       "version" "1.0.0-preview2-final",  
  4.       "type" "build"  
  5.     },   

 In next step, add the following line in “tools” section.

"Microsoft.EntityFrameworkCore.Tools" "1.0.0-preview2-final"

Create Entity Classes

After adding the dependencies for Entity Framework, let’s  create Employee and  Project entity classes. There will be many to one relationship b/w Employee and Project entity, that means one employee will handle only one project at a time but there can be more than one employees in a single project.

Employee Entity

Add an Employee class in Models folder and replace the code of Employee class with the following code.

  1. using System.ComponentModel.DataAnnotations;  
  2. using System.ComponentModel.DataAnnotations.Schema;  
  3. namespace EmployeeDetail.Models  
  4. {  
  5.     public class Employee  
  6.     {  
  7.         [Key]  
  8.         [DatabaseGenerated(DatabaseGeneratedOption.Identity)]  
  9.         public int EmployeeId { get; set; }  
  10.         public string EmployeeName { get; set; }  
  11.         public string Designation { get; set; }  
  12.         public string Skills { get; set; }  
  13.         public int ProjectId { get; set; }   
  14.         public Project Project { get; set; }  
  15.     } }  

In above code, EmployeeId property is primary key for the Employee entity and it is also identity type. The ProjectId is foreign key and corresponding navigation property is Project.

Project Entity

Now, add another class in Model folder and name this class as “Project”, replace the code of this class with following code.

  1. namespace EmployeeDetail.Models  
  2. {  
  3.     public class Project  
  4.     {  
  5.         public int ProjectId { get; set; }  
  6.         public string ProjectName { get; set; }  
  7.         public DateTime StartDate { get; set; }  
  8.         public DateTime EndDate { get; set; }  
  9.   
  10.     }  
  11. }  

The ProjectId property is primary key for this entity and other properties define the information about the project.

Add DbContext Class:

The main class of any Entity Framework structure is DbCntext class in this class we define all the entities that are used to create the table in the database. Now add another class in Model folder and named as “EmployeeContext”. This class will be derived from “System.Data.Entity.DbContext” class. Replace the code this class with following code.

  1. namespace EmployeeDetail.Models  
  2. {  
  3.     public class EmployeeContext DbContext   
  4.      {  
  5.         public EmployeeContext(DbContextOptions<EmployeeContext> options)  base(options)  
  6.         {  
  7.   
  8.         }  
  9.         public DbSet<Employee> Employee { get; set; }  
  10.         public DbSet<Project> Project { get; set; }  
  11.     }  
  12. }  

In the above code, we defined two Dbset properties for Employee and Project class, this property will create two tables in database and name will be same as define in Dbset property.  

Register Context with dependency Injection

After creating all entity  and dbContext classes now we add a service for Entity Framework such that any component require this service can be provided by constructor.  In our controller constructor we use this service to get the context instance. Now open “startup.cs” and add following code to “ConfigureServices” method.

  1. services.AddDbContext<EmployeeContext>(options =>options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));  

 

In the above code we register a service for DbContext, here   “DefaultConnection” providing  the connection string that is defined in our “appsettings.json”. Now open your “appsettings.json” file and add following code. Replace the server name with your SQL Server name.

  1. {  
  2.   "ApplicationInsights" {  
  3.     "InstrumentationKey" ""  
  4.   },  
  5.   "ConnectionStrings" {  
  6.     "DefaultConnection" "Server=******;Database=Employee;Trusted_Connection=True;MultipleActiveResultSets=true"  
  7.   },  
  8.   "Logging" {  
  9.     "IncludeScopes" false,  
  10.     "LogLevel" {  
  11.       "Default" "Debug",  
  12.       "System" "Information",  
  13.       "Microsoft" "Information"  
  14.     }  
  15.   }  
  16. }  

Add Migration and Create Database

After configuring dbContext and entity classes now we add the migration and using the “Update-Database” command we will create our project database. So first add the migration. Open your “Package Manager Console” run “Add-Migration firstMigration” command. This command will add the Migrations folder in your project and first file in Migrations folder contain all information how to create your database.


Now we will run “Update-Database” command, this command take codes available in migration class and updates your database. In our case this command create a “Employee” database in SQL Server that contains the all table and properties that we defined earlier in our model classes.

Now open your SQL Server, you will found that “Employee” database has been added.


Add initial data

Now our tables and database is ready let’s add some initials data into our tables.

Add Data into Project Table

  1. INSERT INTO dbo.Project  
  2. (  
  3.     ProjectName,  
  4.     StartDate,  
  5.      EndDate  
  6. )  
  7. VALUES( N'Lions',CAST('02/01/2017' as datetime),CAST('04/05/2017' AS datetime) ),(  N'OUP',CAST('08/09/2016' AS datetime),CAST('12/03/2017' AS datetime) ),  
  8. (   N'VMesh',CAST('12/04/2016' as date),CAST('04/01/2017'as date)  

 

Add Data into Employee Table

  1. insert into Employee  
  2. (Designation,EmployeeName,ProjectId,Skills)  
  3. values('Developer','Raj Kumar',2,'C#,Asp.Net,MVC'),  
  4. ('Mobile Developer','Ankur Verma',3,'Java, Android Studio, Xamarin'),  
  5. ('Developer','Dheeraj Sharma',4,'C#,Asp.Net,MVC,AngularJS'),  
  6. ('Developer','Dhramveer',3,'C#,Asp.Net,MVC,AngularJS,Node.js'),  
  7. ('Mobile Developer','Shivam Kumar',5,'Java, Android Studio, Xamarin')  

 
Now our database and application setup is ready, let’s start work on controllers and views section of application.

Employee List

After all the configuration is complete, now we start work on the view section of our project. First we create the Employee list and display some basic information of employees.

Add ViewModel

ViewModel in Asp.Net MVC is used to show only required information, ViewModel also used to show data from two or more models. Now we add a ViewModel folder in project. Add a new folder in project and named as “ViewModels”. After creating the “viewModels” folder, add a class in ViewModels folder. Right click on “ViewModels” folder and add a new class, named this class as “Employee_Project”. After creating the class now paste following code in this class. 

  1. public class Employee_Project  
  2.     {  
  3.         public int EmployeeId { get; set; }  
  4.         public string EmployeeName { get; set; }  
  5.         public string Designation { get; set; }  
  6.         public string Project { get; set; }  
  7.   
  8.     }  

In the above code  we added four properties, values for first three property will be received from “Employee” model and value of fourth property will be received from the “Project” model.

Now go to “HomeController” and paste the following code in Index method. 

  1. public async Task<IActionResult> Index()  
  2.         {  
  3.             List<Employee_Project> ilIst = new List<Employee_Project>();  
  4.             var listData = await (from emp in _context.Employee  
  5.                             join pro in _context.Project on emp.ProjectId equals pro.ProjectId  
  6.                             select new  
  7.                             {  
  8.                                 emp.EmployeeId,  
  9.                                 emp.EmployeeName,  
  10.                                 emp.Designation,  
  11.                                 pro.ProjectName  
  12.   
  13.                             }  
  14.                           ).ToListAsync();  
  15.             listData.ForEach(x =>  
  16.             {  
  17.                 Employee_Project Obj = new Employee_Project();  
  18.                 Obj.EmployeeId = x.EmployeeId;  
  19.                 Obj.Designation = x.Designation;  
  20.                 Obj.EmployeeName = x.EmployeeName;  
  21.                 Obj.Project = x.ProjectName;  
  22.                 ilIst.Add(Obj);  
  23.             });  
  24.           
  25.             return View(ilIst);  
  26.         }   

In above lines of code we create an asynchronous method. Asynchronous programming is default mode of Asp.Net Core. Asynchronous programming provide a mechanism to write the code in non-blocking mode. A server has a limited numbers of threads, in high load situations if all the threads are blocked and a new request arrives on the server then result will be  block and stop to respond. To overcome all these situations .Net core comes with an asynchronous programming method where we can write code for .Net Core MVC and EF core in an asynchronous way. Result will be a highly responsive and non-blocking server application. So during this project we will try to write the code in an asynchronous way.

In above code we use four keywords “async”, “Task<IActionResult>”, “await” and “ToListAsync”, all these keyword are used to make a method asynchronous.

  • The “async” keyword is used to define a method as asynchronous, this keyword tells the compiler to generate callbacks for parts of the method body and to automatically create the Task<IActionResult> object that is returned. Here Task<IActionResult> define the return type of an asynchronous method as “IActionResult”

  • The “await” keyword represents ongoing work, the asynchronous method in which await is used must be modified by the async keyword and “await” keyword tell the compiler to wait till the execution of an expression written in await.

  • The “ToListAsync” keyword is an asynchronous version of “ToList” method and tries to execute the expression in an asynchronous way.

In the above code we create an asynchronous index method. In the first line  of code “List<Employee_Project> ilIst” we create a list of “Employee_Project” ViewModel type.  Using linq query we get “EmployeeId”, “EmployeeName”, “Designation”, “ProjectName”  information about all employees and add these value in “iList” object using “ForEach” method. In the last line of code we returned this “iList” to view(return View(ilIst)).

After “Index” method in controller now open “Index” view and paste the following code in “Index” view. 

  1. @model IEnumerable<EmployeeDetail.ViewModels.Employee_Project>  
  2.   
  3. @{  
  4.     ViewData["Title"] = "Index";  
  5. }  
  6.   
  7.   
  8.   
  9. <div class="row">  
  10.     <div class="col-md-12">  
  11.         <h3>Employee Application</h3>  
  12.     </div>  
  13. </div>  
  14. <div class="row">  
  15.     <div class="table-responsive">  
  16.         <table class="table">  
  17.             <thead>  
  18.                 <tr>  
  19.                     <th>  
  20.                         @Html.DisplayNameFor(model => model.EmployeeName)  
  21.                     </th>  
  22.                     <th>  
  23.                         @Html.DisplayNameFor(model => model.Designation)  
  24.                     </th>  
  25.                     <th>  
  26.                         @Html.DisplayNameFor(model => model.Project)  
  27.                     </th>  
  28.                     <th>  
  29.                         Action  
  30.                     </th>  
  31.                 </tr>  
  32.             </thead>  
  33.             <tbody>  
  34.                 @foreach (var item in Model)  
  35.             {  
  36.                     <tr>  
  37.                           
  38.                         <td>  
  39.                             @Html.DisplayFor(modelItem => item.EmployeeName)  
  40.                         </td>  
  41.                         <td>  
  42.                             @Html.DisplayFor(modelItem => item.Designation)  
  43.                         </td>  
  44.                         <td>  
  45.                             @Html.DisplayFor(modelItem => item.Project)  
  46.                         </td>  
  47.                         <td>  
  48.                              
  49.                             <a asp-action="Detail" asp-controller="Home"   
  50.                                asp-route-Empid="@item.EmployeeId"  
  51.                                 class="btn btn-primary">  
  52.                              Detail</a>  
  53.                             <a asp-action="Edit" asp-controller="Home"  
  54.                                asp-route-Empid="@item.EmployeeId"  
  55.                                class="btn btn-success">  
  56.                                 Edit  
  57.                             </a>  
  58.                             <a asp-action="Delete" asp-controller="Home"  
  59.                                asp-route-Empid="@item.EmployeeId"  
  60.                                class="btn btn-danger">  
  61.                                 Delete  
  62.                             </a>  
  63.                         </td>  
  64.                     </tr>  
  65.             }  
  66.         </table>  
  67.     </div>  
  68. </div>  
  69. <div class="row">  
  70.     <div class="col-md-12">  
  71.         <a asp-action="NewEmployee" asp-controller="Home">  
  72.             <button type="button" id="submit" class="btn btn-warning">Add Employee</button>  
  73.         </a>  
  74.     </div>  
  75.     
  76. </div>   

In index view we get the “IEnumerable” object of “Employee_Project” ViewModel and create a list of all employees. For each employee entry we will add “Edit”,”Delete” and “Detail”  option. Using “Edit” option we will redirect to a page where we can edit the information of employee, using “Delete” option we can delete any existing employee and using “Detail” option we can get all information about an employee. We also added an “Add Employee” button using that we can add a new employee entry. Code of Index method and Index view will create below screen.


Add New Employee

Home page of our application contains “Add Employee” button, on click of this button a new screen will open where we can add a new employee entry. Now create “NewEmployee” method in Home controller and paste the following code in this method. 

  1. public async Task<IActionResult> NewEmployee()  
  2.         {  
  3.             List<Project> project_ = new List<Project>();  
  4.             var Project = await (from data in _context.Project  
  5.                            select new  
  6.                            {  
  7.                                ProjectId = data.ProjectId,  
  8.                                ProjectName = data.ProjectName  
  9.                            }).ToListAsync();  
  10.             Project.ForEach(x =>  
  11.             {  
  12.                 Project pro = new Project();  
  13.                 pro.ProjectId = x.ProjectId;  
  14.                 pro.ProjectName = x.ProjectName;  
  15.                 project_.Add(pro);  
  16.             });  
  17.               
  18.   
  19.             return View(project_);  
  20.         }   

In the above code we create an asynchronous method (NewEmployee),  and in this method we are getting the list of all projects and return this list to “AddEmployee” view. On view page using this list we creates a dropdown list that contain name of all project. Now right click on View folder and add a new view , named this view as “AddEmployee”. Now paste following code in this view.

  1. <style>  
  2.     .hidedata {  
  3.         padding-top 50px;  
  4.     }  
  5. </style>  
  6. <div class="row">  
  7.      
  8.     <div class="col-md-12">  
  9.         <div class="col-md-8 col-lg-offset-4">  
  10.             <h3>Enter Employee Details</h3>  
  11.             </div>  
  12.         @{   
  13.             List<SelectListItem> items=new List<SelectListItem>();  
  14.   
  15.             foreach (var data in Model)  
  16.             {  
  17.   
  18.                 items.Add(new SelectListItem()  
  19.                 {  
  20.                     Value = Convert.ToString(data.ProjectId),  
  21.                     Text = data.ProjectName  
  22.                 });  
  23.   
  24.             }  
  25.         }  
  26.         @using (@Html.BeginForm("AddEmployee","Home",FormMethod.Post))  
  27.         {   
  28.         <div class="row hidedata" id="hidDiv">  
  29.             <div class="col-md-6 col-lg-offset-3">  
  30.                 <form class="form-horizontal">  
  31.                     <div class="form-group">  
  32.                         <label for="EmployeeName" class="col-sm-4 control-label">Employee Name</label>  
  33.                         <div class="col-sm-8">  
  34.                             <input type="text" class="form-control" name="EmployeeName" placeholder="Employee Name">  
  35.                         </div>  
  36.                     </div>  
  37.                     <div class="form-group">  
  38.                         <label for="Designation" class="col-sm-4 control-label">Designation</label>  
  39.                         <div class="col-sm-8">  
  40.                             <input type="text" class="form-control" name="Designation" placeholder="Designation">  
  41.                         </div>  
  42.                     </div>  
  43.                     <div class="form-group">  
  44.                         <label for="Skills" class="col-sm-4 control-label">Skills</label>  
  45.                         <div class="col-sm-8">  
  46.                             <input type="text" class="form-control" name="Skills" placeholder="Employee Name">  
  47.                               
  48.                         </div>  
  49.                     </div>  
  50.                     <div class="form-group">  
  51.                         <label for="ProjectId" class="col-sm-4 control-label">Project</label>  
  52.                         <div class="col-sm-8">  
  53.                             @Html.DropDownList("ProjectId", items, "---Select---"new { @class = "form-control" })  
  54.   
  55.                         </div>  
  56.                     </div>  
  57.   
  58.                     <div class="form-group">  
  59.                         <div class="col-sm-offset-4 col-sm-8">  
  60.                             <button type="submit" class="btn btn-info">Submit</button>  
  61.                         </div>  
  62.                     </div>  
  63.                 </form>  
  64.   
  65.             </div>  
  66.         </div>  
  67.         }  
  68.              
  69.         </div>  
  70. </div>   

In above codes we created a list of “SelectListItem” type and bind all the values of projects in this list that we retrieved from controller. Using “Html.BeginForm” html helper we created a html form, in this form we created three textbox for “EmployeeName” , “Designation” and “Skills” property and a dropdown for the “Projects”, in this dropdown we bind list values that we created in earlier.

 
In @Html.BeginForm  we define the action and controller name, method of form is POST type. When we submit this form all field values will submit to “AddEmployee” action of “Home” controller. 


Now add following code in “Home” controller. 

  1. [HttpPost]  
  2.         public IActionResult AddEmployee(IFormCollection form)  
  3.         {  
  4.             Employee Emp = new Employee();  
  5.             Emp.EmployeeName = form["EmployeeName"].ToString();  
  6.             Emp.ProjectId = int.Parse(form["ProjectId"].ToString());  
  7.             Emp.Skills = form["Skills"].ToString();  
  8.             Emp.Designation = form["Designation"].ToString();  
  9.             _context.Employee.Add(Emp);  
  10.             _context.SaveChanges();  
  11.             return RedirectToAction("Index");  
  12.   
  13.   
  14.         }   

Here we created “AddEmployee” action. Remember that we have already an action with “AddEmployee” name but type of this action  is POST and takes a parameter of IFormCollection” type. This parameter contains values of all controllers that are presents in the form section of “NewEmployee” view. In “POST” method of “AddEmployee” we get value of all controller from “IformCollection” object. This object contains values in the form of key-value pairs. After fetching out all the values from “IFormCollecion” object we assign these values to Employee object and add this employee object to “_context.Employee” using add method. After adding the new employee we redirect the control to “Index” action.  






Get Employee Details

In index view we are display the list of all employee and each employee entry also contains “Detail” button .


Using this button we can get all information of an employee. Actually this button is anchor tag and this anchor tag contains controller and action name. So when we click on this anchor tag it will redirect to “Detail” action of “Home” controller . We want to display the information of a particular employee so we need to pass the employeeId to our action. In anchor tag we also define the   “asp-route-Empid” attribute , this attribute will add a EmpId parameter to URL string.  

  1. <a asp-action="Detail" asp-controller="Home"   
  2.                                asp-route-Empid="@item.EmployeeId"  
  3.                                 class="btn btn-primary">  
  4.                              Detail</a>   

Let’s  add a “Details” action in Home controller and paste following code in this action. 

  1. public async Task<IActionResult> Detail(int Empid)  
  2.         {  
  3.             EmployeeDetails Empobj = new EmployeeDetails();  
  4.             var EmpDeatils =  await (from emp in _context.Employee  
  5.                               join pro in _context.Project on emp.ProjectId equals pro.ProjectId  
  6.                               where emp.EmployeeId==Empid  
  7.                               select new EmployeeDetails  
  8.                               {  
  9.                                 EmployeeId=  emp.EmployeeId,  
  10.                                 EmployeeName= emp.EmployeeName,  
  11.                                Designation= emp.Designation,  
  12.                                  Skills= emp.Skills,  
  13.                                 ProjectName= pro.ProjectName,  
  14.                                StartDate= pro.StartDate,  
  15.                                  EndDate= pro.EndDate  
  16.   
  17.                               }  
  18.                           ).FirstAsync();  
  19.               
  20.   
  21.             return View(EmpDeatils);  
  22.         }  

In the above code we create an asynchronous action, this action takes an “EmpId” parameter that we passed from the index view. Using this “EmpId” we get all information about that particular employee and return  to the ”Detail” view. In the first line of code we created an object of “EmployeeDetails” class, here “EmployeeDetails” is a ViewModel. We need to create a ViewModel because we want to display personal and project level information of an employee, these information are related to more than one entities. So right click on “ViewModels” folder add a new class and named this class as “EmployeeDetails”. After creating the class paste following code into this class.

  1. namespace EmployeeDetail.ViewModels  
  2. {  
  3.     public class EmployeeDetails  
  4.     {  
  5.         public int EmployeeId { get; set; }  
  6.         public string EmployeeName { get; set; }  
  7.         public string Designation { get; set; }  
  8.         public string Skills { get; set; }  
  9.         public string ProjectName { get; set; }  
  10.         public DateTime StartDate { get; set; }  
  11.         public DateTime EndDate { get; set; }  
  12.   
  13.          
  14.     }  
  15. }  

In “EmployeeDetail” ViewModel we defines all the properties that we will display on details page. After working on controller and ViewModels now we add a new views to Home folder. Right click on Home folder and add a new view and names this view as “Details”. After creating the view now paste following code into view.

  1. @model EmployeeDetail.ViewModels.EmployeeDetails  
  2. @{  
  3.     ViewData["Title"] = "Index";  
  4. }  
  5. <style>  
  6.     .row {  
  7.         line-height 30px;  
  8.         font-size 16px;  
  9.     }  
  10. </style>  
  11. <div class="row">  
  12.     <div class="col-md-12">  
  13.         <h3>Employee Details</h3>  
  14.     </div>  
  15. </div>  
  16.   
  17.   
  18.   
  19. <div class="row">  
  20.     <div class="col-md-3"><b>Employee Name</b></div><div class="col-md-3">@Model.EmployeeName</div>  
  21. </div>  
  22. <div class="row">  
  23.     <div class="col-md-3"><b>Designation</b></div><div class="col-md-3">@Model.Designation</div>  
  24. </div>  
  25.       
  26. <div class="row">  
  27.     <div class="col-md-3"><b>Skills</b></div><div class="col-md-3">@Model.Skills</div>  
  28. </div>  
  29. <div class="row">  
  30.     <div class="col-md-3"><b>ProjectName:</b></div><div class="col-md-3">@Model.ProjectName</div>  
  31. </div>  
  32. <div class="row">  
  33.     <div class="col-md-3"><b>Start Date</b></div><div class="col-md-3">@Model.StartDate.Date</div>  
  34. </div>  
  35. <div class="row">  
  36.     <div class="col-md-3"><b>End Date</b></div><div class="col-md-3">@Model.EndDate.Date</div>  
  37. </div>  

Detail view will display the personal and project level information of an employee. For example when we click on “Detail” button for “Dheeraj Sharma” employee, we will redirect to Detail page and following result will be display.


Update Employee Details

In employee listing page we added an edit button for each employee. When any user clicks on this button it will redirect to “Edit” action of the “HomeController” and we are passing the “EmployeeId” as parameter.

  1. <a asp-action="Edit" asp-controller="Home"  
  2.                                asp-route-Empid="@item.EmployeeId"  
  3.                                class="btn btn-success">  
  4.                                 Edit  
  5.                             </a>   

Now add following code in “HomeController”.

  1. public async Task<IActionResult> Edit(int Empid)  
  2.         {  
  3.             EmployeeInfo empData = new EmployeeInfo();  
  4.             var empObj= await  
  5.                  _context.Employee  
  6.                 .Where(x => x.EmployeeId == Empid)  
  7.                 .FirstAsync();  
  8.   
  9.             empData.EmployeeId = empObj.EmployeeId;  
  10.             empData.EmployeeName = empObj.EmployeeName;  
  11.             empData.Skills = empObj.Skills;  
  12.             empData.Designation = empObj.Designation;  
  13.             empData.ProjectId = empObj.ProjectId;  
  14.             var projectObj = _context.Project;  
  15.   
  16.   
  17.              
  18.            await projectObj.ForEachAsync(x=>{  
  19.                 empData.list.Add(new SelectListItem() { Text = x.ProjectName, Value = x.ProjectId.ToString() });  
  20.             });  
  21.               
  22.             return View(empData);  
  23.         }  

In the above code we created an asynchronous action and return type of action is “Task<IActionResult>”. In the first line of code  we are creating the object of “EmployeeInfo” class. We insert the employee details and a list of SelectListItem type in this object. Now right click on “Models”  folder, add a new class and named this class as “EmployeeInfo”. Now paste following code in this class.

  1. public class EmployeeInfo  
  2.     {  
  3.         public int EmployeeId { get; set; }  
  4.         public string EmployeeName { get; set; }  
  5.         public string Designation { get; set; }  
  6.         public string Skills { get; set; }  
  7.         public int ProjectId { get; set; }  
  8.   
  9.         public List<SelectListItem> list = new List<SelectListItem>();  
  10.     }  

This class will be used as Model class for our “Edit” view. In this class we added some property and List of “SelectListItem” type. This list holds all the project names.

In next lines of “Edit” action we fetched out the employee details and project list and bind all these values to “empData” object of EmployeeDetails class and in last line  we returned  this object to view.

After that now right click on Views folder, add a new view and name this view as “Edit and paste following code into this view.

  1. <style>  
  2.     .hidedata {  
  3.         padding-top 50px;  
  4.     }  
  5. </style>  
  6. <div class="row">  
  7.   
  8.     <div class="col-md-12">  
  9.         <div class="col-md-8 col-lg-offset-4">  
  10.             <h3>Edit Employee Details</h3>  
  11.         </div>  
  12.           
  13.         @using (@Html.BeginForm("Edit""Home", FormMethod.Post))  
  14.         {  
  15.             <div class="row hidedata" id="hidDiv">  
  16.                 <div class="col-md-6 col-lg-offset-3">  
  17.                     <form class="form-horizontal">  
  18.                         <div class="form-group">  
  19.                             <label for="EmployeeName" class="col-sm-4 control-label">Employee Name</label>  
  20.                             <div class="col-sm-8">  
  21.                                 <input type="text" class="form-control" value="@Model.EmployeeName" name="EmployeeName" placeholder="Employee Name">  
  22.                             </div>  
  23.                         </div>  
  24.                         <div class="form-group">  
  25.                             <label for="Designation" class="col-sm-4 control-label">Designation</label>  
  26.                             <div class="col-sm-8">  
  27.                                 <input type="text" class="form-control" value="@Model.Designation" name="Designation" placeholder="Designation">  
  28.                             </div>  
  29.                         </div>  
  30.                         <div class="form-group">  
  31.                             <label for="Skills" class="col-sm-4 control-label">Skills</label>  
  32.                             <div class="col-sm-8">  
  33.                                 <input type="text" class="form-control" value="@Model.Skills" name="Skills" placeholder="Employee Name">  
  34.   
  35.                             </div>  
  36.                         </div>  
  37.                         <div class="form-group">  
  38.                             <label for="ProjectId" class="col-sm-4 control-label">Project</label>  
  39.                             <div class="col-sm-8">  
  40.                                 @Html.DropDownList("ProjectId"new SelectList(Model.list,"Value","Text",Model.ProjectId) , "---Select---"new { @class = "form-control" })  
  41.                             </div>  
  42.                         </div>  
  43.                        <input type="hidden" name="EmployeeId" value="@Model.EmployeeId"/>  
  44.                         <div class="form-group">  
  45.                             <div class="col-sm-offset-4 col-sm-8">  
  46.                                 <button type="submit" class="btn btn-info">Submit</button>  
  47.                             </div>  
  48.                         </div>  
  49.                     </form>  
  50.   
  51.                 </div>  
  52.             </div>  
  53.         }  
  54.   
  55.     </div>  
  56. </div>   

Above HTML code creates an html form, in this form we added some controller. In initial state values of all these controller are filled with the Model data that we returned  from the “Edit” action.


After any modification when we submit all the changes it will redirect to “Edit” action of Home Controller. Now add following code in your “HomeController”. 

  1. [HttpPost]  
  2.         public IActionResult Edit(IFormCollection form)  
  3.         {  
  4.             Employee emp = new Employee();  
  5.             emp.EmployeeId = int.Parse(form["EmployeeId"]);  
  6.             emp.Designation = form["Designation"];  
  7.             emp.EmployeeName = form["EmployeeName"];  
  8.             emp.ProjectId = int.Parse(form["ProjectId"]);  
  9.             emp.Skills = form["Skills"];  
  10.             _context.Entry(emp).State = Microsoft.EntityFrameworkCore.EntityState.Modified;  
  11.             _context.SaveChanges();  
  12.             return RedirectToAction("Index");  
  13.         }  

In above code we are getting all the information from “IformCollection” object and adding this information in Employee object. Using this “Employee” object we are updating the state of Employee and saving all these changes to the database.


Delete Employee Entry

In Employee listing page we have an option to delete any employee entry. When we click on delete button it will redirect to “Delete” action of “Home” controller. We are also passing the “Empid” parameter that contain Employee Id to whom we want to delete.

  1. <a asp-action="Delete" asp-controller="Home"  
  2.                                asp-route-Empid="@item.EmployeeId"  
  3.                                class="btn btn-danger">  
  4.                                 Delete  
  5.                             </a>   

Now add following code in your “HomeController”.

  1. public IActionResult Delete(int Empid)  
  2.         {  
  3.   
  4.             Employee Emp;  
  5.             Emp = _context.Employee.Where(x => x.EmployeeId == Empid).First();  
  6.             _context.Employee.Remove(Emp);  
  7.             _context.SaveChanges();  
  8.             return RedirectToAction("Index");  
  9.         }  

In above line of codes according to “EmployeeId” value we are getting the Employee object and remove this employee from dbContext and save all the changes to database. In short form  all the above code delete the employee entry and save all the changes in database.

Add Search Box

Now we add search functionality in our project, for this we will add a textbox and search button. We perform the search on Employee name and project name. Replace below code with code of your “Index” view.

  1. @model IEnumerable<EmployeeDetail.ViewModels.Employee_Project>  
  2.   
  3. @{  
  4.     ViewData["Title"] = "Index";  
  5. }  
  6.   
  7.   
  8.   
  9. <div class="row">  
  10.     <div class="col-md-12">  
  11.         <h3>Employee Application</h3>  
  12.     </div>  
  13. </div>  
  14. <div class="row">  
  15.     <div class="col-md-12">  
  16.         <form asp-action="Search" >  
  17.             <div class="form-actions no-color">  
  18.                 <p>  
  19. <input type="text" name="searchText" value="@ViewData["searchText"]" />  
  20.                     <input type="submit" value="Search" class="btn btn-primary" /> |  
  21.                     <a asp-action="Index">Show All</a>  
  22.                 </p>  
  23.             </div>  
  24.         </form>  
  25.     </div>  
  26. </div>  
  27. <div class="row">  
  28.     <div class="table-responsive">  
  29.         <table class="table">  
  30.             <thead>  
  31.                 <tr>  
  32.                     <th>  
  33.                         @Html.DisplayNameFor(model => model.EmployeeName)  
  34.                     </th>  
  35.                     <th>  
  36.                         @Html.DisplayNameFor(model => model.Designation)  
  37.                     </th>  
  38.                     <th>  
  39.                         @Html.DisplayNameFor(model => model.Project)  
  40.                     </th>  
  41.                     <th>  
  42.                         Action  
  43.                     </th>  
  44.                 </tr>  
  45.             </thead>  
  46.             <tbody>  
  47.                 @foreach (var item in Model)  
  48.             {  
  49.                     <tr>  
  50.                           
  51.                         <td>  
  52.                             @Html.DisplayFor(modelItem => item.EmployeeName)  
  53.                         </td>  
  54.                         <td>  
  55.                             @Html.DisplayFor(modelItem => item.Designation)  
  56.                         </td>  
  57.                         <td>  
  58.                             @Html.DisplayFor(modelItem => item.Project)  
  59.                         </td>  
  60.                         <td>  
  61.                              
  62.                             <a asp-action="Detail" asp-controller="Home"   
  63.                                asp-route-Empid="@item.EmployeeId"  
  64.                                 class="btn btn-primary">  
  65.                              Detail</a>  
  66.                             <a asp-action="Edit" asp-controller="Home"  
  67.                                asp-route-Empid="@item.EmployeeId"  
  68.                                class="btn btn-success">  
  69.                                 Edit  
  70.                             </a>  
  71.                             <a asp-action="Delete" asp-controller="Home"  
  72.                                asp-route-Empid="@item.EmployeeId"  
  73.                                class="btn btn-danger">  
  74.                                 Delete  
  75.                             </a>  
  76.                         </td>  
  77.                     </tr>  
  78.             }  
  79.         </table>  
  80.     </div>  
  81. </div>  
  82. <div class="row">  
  83.     <div class="col-md-12">  
  84.         <a asp-action="NewEmployee" asp-controller="Home">  
  85.             <button type="button" id="submit" class="btn btn-warning">Add Employee</button>  
  86.         </a>  
  87.     </div>  
  88.     
  89. </div>  


For searching functionality we added a form section. In this form we have a textbox, when we insert some text in textbox and click on “Search” button it will redirect to “Search” action of “HomeController”.

  1. <form asp-action="Search" >  
  2.             <div class="form-actions no-color">  
  3.                 <p>  
  4. <input type="text" name="searchText" value="@ViewData["searchText"]" />                    <input type="submit" value="Search" class="btn btn-primary" /> |  
  5.                     <a asp-action="Index">Show All</a>  
  6.                 </p>  
  7.             </div>  
  8.         </form>  

Now in “HomeController” add following code.

  1. public async Task<IActionResult> Search(IFormCollection form)  
  2.         {  
  3.             if (form["searchText"].ToString() != "")  
  4.             {  
  5.                 List<Employee_Project> ilIst = new List<Employee_Project>();  
  6.                 var listData = await(from emp in _context.Employee  
  7.                                      join pro in _context.Project on emp.ProjectId equals pro.ProjectId  
  8.                                      where emp.EmployeeName.Contains(form["searchText"].ToString())   
  9.                                      ||  
  10.                                      pro.ProjectName.Contains(form["searchText"].ToString())                  
  11.                                      select new  
  12.                                      {  
  13.                                          emp.EmployeeId,  
  14.                                          emp.EmployeeName,  
  15.                                          emp.Designation,  
  16.                                          pro.ProjectName  
  17.   
  18.                                      }  
  19.                               ).ToListAsync();  
  20.                 listData.ForEach(x =>  
  21.                 {  
  22.                     Employee_Project Obj = new Employee_Project();  
  23.                     Obj.EmployeeId = x.EmployeeId;  
  24.                     Obj.Designation = x.Designation;  
  25.                     Obj.EmployeeName = x.EmployeeName;  
  26.                     Obj.Project = x.ProjectName;  
  27.                     ilIst.Add(Obj);  
  28.                 });  
  29.   
  30.                 return View("Index",ilIst);  
  31.   
  32.             }  
  33.             else  
  34.             {  
  35.                 return RedirectToAction("Index");  
  36.             }  
  37.         }  

In above code we creates a “Search” action of asynchronous type, in this action we use the “IFormCollection” object to get the value of search textbox. If values textbox is not empty then  we perform the “Contains” operator in Linq query that is equal to the like operator of SQL. This LINQ query return all the Employee whose EmployeeName or project name contains the text that we inserted into textbox.

       
We have also a “Show All” option using this option we can get list of all the employees.

Add Sorting Functionality

After adding the search functionality now we will add data sorting option in employee list. We will perform the sort operation on Employee name. By default sorting order will be ascending. So add “order by emp.EmployeeName ascending” line in index action where we are getting the list employees.


This command sorts the data in ascending order according the Employees name.



Add following lines of code in “EmployeeName” header of table. 

  1. <a asp-action="Sort" asp-route-sortOrder="@ViewData["sortOrder"]">  
  2.                             <img src=@("/images/"+(ViewData["sortOrder"]!=null?ViewData["sortOrder"]"ascending")+".png") />  
  3.                         </a>  


In above code, we added an image and we will show the “ascending.png” image for ascending order sorting and “descending.png” images for the descending order sorting. Now add two images for up and down  arrow  in your images folder and rename these images to “descending.png” and “ascending.png”. We will use these images to indicate the sorting order.

Now, add “Sort” action in “HomeController” and copy following code in “Sort” action.

  1. public async Task<IActionResult> Sort(string sortOrder)  
  2.             {  
  3.             List<Employee_Project> ilIst = new List<Employee_Project>();  
  4.               
  5.             if (sortOrder == "ascending" || sortOrder == null)  
  6.             {  
  7.                 ViewData["sortOrder"] = "descending";  
  8.                var listData = await (from emp in _context.Employee  
  9.                                       join pro in _context.Project on emp.ProjectId equals pro.ProjectId  
  10.   
  11.                                       orderby emp.EmployeeName descending  
  12.                                       select new  
  13.                                       {  
  14.                                           emp.EmployeeId,  
  15.                                           emp.EmployeeName,  
  16.                                           emp.Designation,  
  17.                                           pro.ProjectName  
  18.   
  19.                                       }  
  20.   
  21.                               ).ToListAsync();  
  22.                 listData.ForEach(x =>  
  23.                 {  
  24.                     Employee_Project Obj = new Employee_Project();  
  25.                     Obj.EmployeeId = x.EmployeeId;  
  26.                     Obj.Designation = x.Designation;  
  27.                     Obj.EmployeeName = x.EmployeeName;  
  28.                     Obj.Project = x.ProjectName;  
  29.                     ilIst.Add(Obj);  
  30.                 });  
  31.   
  32.                 return View("Index", ilIst);  
  33.             }  
  34.             else  
  35.             {  
  36.                 ViewData["sortOrder"] = "ascending";  
  37.                 var  listData = await (from emp in _context.Employee  
  38.                                       join pro in _context.Project on emp.ProjectId equals pro.ProjectId  
  39.   
  40.                                       orderby emp.EmployeeName ascending  
  41.                                       select new  
  42.                                       {  
  43.                                           emp.EmployeeId,  
  44.                                           emp.EmployeeName,  
  45.                                           emp.Designation,  
  46.                                           pro.ProjectName  
  47.   
  48.                                       }  
  49.                                       ).ToListAsync();  
  50.                 listData.ForEach(x =>  
  51.                 {  
  52.                     Employee_Project Obj = new Employee_Project();  
  53.                     Obj.EmployeeId = x.EmployeeId;  
  54.                     Obj.Designation = x.Designation;  
  55.                     Obj.EmployeeName = x.EmployeeName;  
  56.                     Obj.Project = x.ProjectName;  
  57.                     ilIst.Add(Obj);  
  58.                 });  
  59.   
  60.                 return View("Index", ilIst);  
  61.   
  62.             }  
  63.               
  64.   
  65.               
  66.               
  67.         }  

According to the value of “sortOrder” parameter, we will perform the orderBy clause. When value of “sortOrder” parameter is ascending or null, then we will sort the employee list in descending order and show the “descending.png” image.


In else statement, we perform the sorting of the list in “ascending” order and show the “ascending.png” image.



Conclusion            

In this article, we performed the CRUD operation in .NET Core using the EF Core. I hope you liked this article. If you have any query, please write it in the comment section.