<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ entity framework core - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ entity framework core - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sun, 24 May 2026 22:25:33 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/entity-framework-core/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to deploy editable tables in HTML using Angular 5 and Entity Framework Core ]]>
                </title>
                <description>
                    <![CDATA[ By Ankit Sharma Introduction In this article, we are going to create a web application using ASP.NET Core 2.0 and Angular 5 with the help of the Entity Framework (EF) Core database-first approach. We will be creating a sample Employee Record Manageme... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/asp-net-core-crud-using-angular-5-and-entity-framework-core-374ac04cd3ec/</link>
                <guid isPermaLink="false">66d45d9cc7632f8bfbf1e3d9</guid>
                
                    <category>
                        <![CDATA[ angular 5 ]]>
                    </category>
                
                    <category>
                        <![CDATA[ entity framework core ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Wed, 18 Apr 2018 05:56:43 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*W5vbBi1Nah40KGMRIE1GJw.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Ankit Sharma</p>
<h3 id="heading-introduction">Introduction</h3>
<p>In this article, we are going to create a web application using <strong>ASP.NET Core 2.0</strong> and <strong>Angular 5</strong> with the help of the Entity Framework (EF) Core database-first approach. We will be creating a sample Employee Record Management system. To read the inputs from the user, we will use <strong>Angular forms</strong> with required field validations on the client side. We are also going to bind a dropdown list in the Angular form to a table in the database using EF Core.</p>
<p>We will be using <strong>Visual Studio 2017</strong> and <strong>SQL Server</strong> version 2008 or above.</p>
<h3 id="heading-prerequisites">Prerequisites</h3>
<ul>
<li>Install .NET Core 2.0.0 or above SDK from <a target="_blank" href="https://www.microsoft.com/net/core#windowscmd">here</a>.</li>
<li>Install the latest version of Visual Studio 2017 Community Edition from <a target="_blank" href="https://www.visualstudio.com/downloads/">here</a>.</li>
<li>Download and install the latest version of Node.js from <a target="_blank" href="https://nodejs.org/en/download/">here</a>..</li>
<li>SQL Server 2008 or above.</li>
</ul>
<h3 id="heading-source-code">Source Code</h3>
<p>Before proceeding, I recommend you get the source code from <a target="_blank" href="https://github.com/AnkitSharma-007/CRUD.ASPCore.Angular5.WebAPI.EF">Github</a>.</p>
<h3 id="heading-creating-the-tables">Creating the tables</h3>
<p>We will be using two tables to store our data.</p>
<ol>
<li><code>tblEmployee</code>: used to store the details of employees. It contains fields such as EmployeeID, Name, City, Department, and Gender.</li>
<li><code>tblCities</code>: this contains the list of cities. It is used to populate the <em>City</em> field of the tblEmployee table. tblCities contains two fields, CityID and CityName.</li>
</ol>
<p>Execute the following commands to create both tables:</p>
<pre><code class="lang-sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> tblEmployee (  EmployeeID <span class="hljs-built_in">int</span> <span class="hljs-keyword">IDENTITY</span>(<span class="hljs-number">1</span>,<span class="hljs-number">1</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> PRIMARY <span class="hljs-keyword">KEY</span>,  <span class="hljs-keyword">Name</span> <span class="hljs-built_in">varchar</span>(<span class="hljs-number">20</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> ,  City <span class="hljs-built_in">varchar</span>(<span class="hljs-number">20</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> ,  Department <span class="hljs-built_in">varchar</span>(<span class="hljs-number">20</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> ,  Gender <span class="hljs-built_in">varchar</span>(<span class="hljs-number">6</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span>   )  <span class="hljs-keyword">GO</span>    <span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> tblCities (  CityID <span class="hljs-built_in">int</span> <span class="hljs-keyword">IDENTITY</span>(<span class="hljs-number">1</span>,<span class="hljs-number">1</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span> PRIMARY <span class="hljs-keyword">KEY</span>,  CityName <span class="hljs-built_in">varchar</span>(<span class="hljs-number">20</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-literal">NULL</span>   )  <span class="hljs-keyword">GO</span>
</code></pre>
<p>Now we will put some data into the <code>tblCities</code> table. We will be using this table to bind a dropdown list in our web application. The desired city can be selected using this dropdown list. Use the following insert statements.</p>
<pre><code class="lang-sql"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> tblCities <span class="hljs-keyword">VALUES</span>(<span class="hljs-string">'New Delhi'</span>);  <span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> tblCities <span class="hljs-keyword">VALUES</span>(<span class="hljs-string">'Mumbai'</span>);  <span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> tblCities <span class="hljs-keyword">VALUES</span>(<span class="hljs-string">'Hyderabad'</span>);  <span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> tblCities <span class="hljs-keyword">VALUES</span>(<span class="hljs-string">'Chennai'</span>);  <span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> tblCities <span class="hljs-keyword">VALUES</span>(<span class="hljs-string">'Bengaluru'</span>);
</code></pre>
<p>Now, our Database part has been completed. So, we will proceed to create the MVC application using Visual Studio 2017.</p>
<h3 id="heading-create-the-aspnet-mvc-web-application">Create the ASP.NET MVC web application</h3>
<p>Open Visual Studio and select File &gt;&gt; New &gt;&gt; Project.</p>
<p>After selecting the project, a “New Project” dialog will open. Select .NET Core inside Visual C# menu from the left panel.</p>
<p>Then, select “ASP.NET Core Web Application” from the available project types. Put the name of the project as “EFNgApp” and press OK.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/YVcPWRXi6XvKor9-rq6xKvKl1Q9D5E90SDEi" alt="Image" width="650" height="397" loading="lazy"></p>
<p>After clicking on OK, a new dialog will open asking you to select the project template. You can observe two drop-down menus at the top left of the template window. Select “.NET Core” and “ASP.NET Core 2.0” from these dropdowns. Then, select the “Angular” template and press OK.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/K6YQvZWOFaTVBg9tQW-2ZgTokPtEbaAyxbNC" alt="Image" width="650" height="425" loading="lazy"></p>
<p>Now, our project will be created. You can see the folder structure in Solution Explorer in the below image.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/iCGpODkNimBqGNlUVEJPy0468-Rnjv7-N1ih" alt="Image" width="245" height="520" loading="lazy"></p>
<p>Here, we have our “Controllers” and “Views” folders. We won’t be touching the Views folder for this tutorial, since we will be using Angular to handle the UI.</p>
<p>The <em>Controllers</em> folders will contain our Web API controller. The point of interest for us is the “ClientApp” folder where the client side of our application resides.</p>
<p>Inside the “ClientApp/app/components folder,” we already have few components created. These are provided by default with the Angular template in VS 2017. These components won’t affect our application, but for the sake of this tutorial, we will delete the “fetchdata” and “counter” folders from ClientApp/app/components<em>.</em></p>
<h3 id="heading-adding-the-model-to-the-application">Adding the Model to the Application</h3>
<p>We are using Entity Framework core database first approach to create our models. Navigate to Tools &gt;&gt; NuGet Package Manager &gt;&gt; Package Manager Console.</p>
<p>We have to install the package for the database provider that we are targeting, which is SQL Server in this case. Now run the following command:</p>
<pre><code>Install-Package Microsoft.EntityFrameworkCore.SqlServer
</code></pre><p>Since we are using Entity Framework Tools to create a model from the existing database, we will install the tools package as well. Run the following command:</p>
<pre><code>Install-Package Microsoft.EntityFrameworkCore.Tools
</code></pre><p>After you have installed both the packages, we will scaffold our model from the database tables using the following command:</p>
<pre><code>Scaffold-DbContext <span class="hljs-string">"Your connection string here"</span> Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Tables tblEmployee, tblCities
</code></pre><p><strong>Do not forget</strong> to put your own connection string (inside double quotes “ ”). After this command gets executed successfully, a “Models” folder will be created. This folder contains three class files: <code>myTestDBContext.cs</code><em>,</em> <code>TblCities.cs</code> and <code>TblEmployee.cs</code>. We have successfully created our Models using EF core database first approach.</p>
<p>Now, we will create one more class file to handle database related operations</p>
<p>Right click on the “Models” folder and select Add &gt;&gt; Class. Name your <code>class EmployeeDataAccessLa</code>_y_er.cs and click the “Add” button. At this point, the “Models” folder will have the following structure.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/bovuU80LNmD-dy2hiWJTqQ5N11RnqoiFv4Oa" alt="Image" width="248" height="398" loading="lazy"></p>
<p>Open “EmployeeDataAccessLayer.cs” and insert the following code to handle database operations.</p>
<pre><code>using Microsoft.EntityFrameworkCore;  using System;  using System.Collections.Generic;  using System.Linq;  using System.Threading.Tasks;    namespace EFNgApp.Models  {      public <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">EmployeeDataAccessLayer</span>      </span>{          myTestDBContext db = <span class="hljs-keyword">new</span> myTestDBContext();            public IEnumerable&lt;TblEmployee&gt; GetAllEmployees()          {              <span class="hljs-keyword">try</span>              {                  <span class="hljs-keyword">return</span> db.TblEmployee.ToList();              }              <span class="hljs-keyword">catch</span>              {                  <span class="hljs-keyword">throw</span>;              }          }            <span class="hljs-comment">//To Add new employee record           public int AddEmployee(TblEmployee employee)          {              try              {                  db.TblEmployee.Add(employee);                  db.SaveChanges();                  return 1;              }              catch              {                  throw;              }          }            //To Update the records of a particluar employee          public int UpdateEmployee(TblEmployee employee)          {              try              {                  db.Entry(employee).State = EntityState.Modified;                  db.SaveChanges();                    return 1;              }              catch              {                  throw;              }          }            //Get the details of a particular employee          public TblEmployee GetEmployeeData(int id)          {              try              {                  TblEmployee employee = db.TblEmployee.Find(id);                  return employee;              }              catch              {                  throw;              }          }            //To Delete the record of a particular employee          public int DeleteEmployee(int id)          {              try              {                  TblEmployee emp = db.TblEmployee.Find(id);                  db.TblEmployee.Remove(emp);                  db.SaveChanges();                  return 1;              }              catch              {                  throw;              }          }            //To Get the list of Cities          public List&lt;TblCities&gt; GetCities()          {              List&lt;TblCities&gt; lstCity = new List&lt;TblCities&gt;();              lstCity = (from CityList in db.TblCities select CityList).ToList();                return lstCity;          }      }  }</span>
</code></pre><p>Now, we will proceed to create our Web API Controller.</p>
<h3 id="heading-adding-the-web-api-controller-to-the-application">Adding the Web API Controller to the Application</h3>
<p>Right click on Controllers folder and select Add &gt;&gt; New Item.</p>
<p>An “Add New Item” dialog box will open. Select “ASP.NET<em>”</em> from the left panel. Then, select “Web API Controller Class” from templates panel and put the name as <code>EmployeeController.cs</code>. Click “Add”.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/q623LWrLWzNKZSYDuGzALTxR0OxuLWmYey-G" alt="Image" width="650" height="397" loading="lazy"></p>
<p>This will create our Web API “EmployeeController” class. We will put all our business logic in this controller. We will call the methods of “EmployeeDataAccessLayer” to fetch data and pass on the data to the Angular frontend.</p>
<p>Open the “EmployeeController.cs” file and insert the following code into it:</p>
<pre><code>using System;  using System.Collections.Generic;  using System.Linq;  using System.Threading.Tasks;  using EFNgApp.Models;  using Microsoft.AspNetCore.Mvc;     namespace EFNgApp.Controllers  {        public <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">EmployeeController</span> : <span class="hljs-title">Controller</span>      </span>{          EmployeeDataAccessLayer objemployee = <span class="hljs-keyword">new</span> EmployeeDataAccessLayer();            [HttpGet]          [Route(<span class="hljs-string">"api/Employee/Index"</span>)]          public IEnumerable&lt;TblEmployee&gt; Index()          {              <span class="hljs-keyword">return</span> objemployee.GetAllEmployees();          }            [HttpPost]          [Route(<span class="hljs-string">"api/Employee/Create"</span>)]          public int Create([FromBody] TblEmployee employee)          {              <span class="hljs-keyword">return</span> objemployee.AddEmployee(employee);          }            [HttpGet]          [Route(<span class="hljs-string">"api/Employee/Details/{id}"</span>)]          public TblEmployee Details(int id)          {              <span class="hljs-keyword">return</span> objemployee.GetEmployeeData(id);          }            [HttpPut]          [Route(<span class="hljs-string">"api/Employee/Edit"</span>)]          public int Edit([FromBody]TblEmployee employee)          {              <span class="hljs-keyword">return</span> objemployee.UpdateEmployee(employee);          }            [HttpDelete]          [Route(<span class="hljs-string">"api/Employee/Delete/{id}"</span>)]          public int Delete(int id)          {              <span class="hljs-keyword">return</span> objemployee.DeleteEmployee(id);          }            [HttpGet]          [Route(<span class="hljs-string">"api/Employee/GetCityList"</span>)]          public IEnumerable&lt;TblCities&gt; Details()          {              <span class="hljs-keyword">return</span> objemployee.GetCities();          }      }  }
</code></pre><p>We are done with our backend logic. Now we’ll code our frontend using Angular 5.</p>
<h3 id="heading-create-the-angular-service">Create the Angular Service</h3>
<p>We will create an Angular service which will convert the Web API response to JSON and pass it to our component.</p>
<p>Right-click on the “ClientApp/app” folder and then Add &gt;&gt; New Folder and name the folder as “Services”.</p>
<p>Right-click on the “Services” folder and select Add &gt;&gt; New Item. An “Add New Item” dialog box will open. Select “Scripts” from the left panel. Then select “TypeScript File” from the templates panel, and put the na<code>me as empservice.serv</code>ice.ts. Click “Add”.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/XNeVx14RTmuHKuPLcemx4ZKNwivkQCYLY0kX" alt="Image" width="650" height="397" loading="lazy"></p>
<p>Open the “empservice.service.ts” file and insert the following code into it:</p>
<pre><code><span class="hljs-keyword">import</span> { Injectable, Inject } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>;  <span class="hljs-keyword">import</span> { Http, Response } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/http'</span>;  <span class="hljs-keyword">import</span> { Observable } <span class="hljs-keyword">from</span> <span class="hljs-string">'rxjs/Observable'</span>;  <span class="hljs-keyword">import</span> { Router } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/router'</span>;  <span class="hljs-keyword">import</span> <span class="hljs-string">'rxjs/add/operator/map'</span>;  <span class="hljs-keyword">import</span> <span class="hljs-string">'rxjs/add/operator/catch'</span>;  <span class="hljs-keyword">import</span> <span class="hljs-string">'rxjs/add/observable/throw'</span>;    @Injectable()  <span class="hljs-keyword">export</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">EmployeeService</span> </span>{      myAppUrl: string = <span class="hljs-string">""</span>;        <span class="hljs-keyword">constructor</span>(private _http: Http, @Inject('BASE_URL') baseUrl: string) {          <span class="hljs-built_in">this</span>.myAppUrl = baseUrl;      }        getCityList() {          <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>._http.get(<span class="hljs-built_in">this</span>.myAppUrl + <span class="hljs-string">'api/Employee/GetCityList'</span>)              .map(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> res.json())              .catch(<span class="hljs-built_in">this</span>.errorHandler);      }        getEmployees() {          <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>._http.get(<span class="hljs-built_in">this</span>.myAppUrl + <span class="hljs-string">'api/Employee/Index'</span>)              .map(<span class="hljs-function">(<span class="hljs-params">response: Response</span>) =&gt;</span> response.json())              .catch(<span class="hljs-built_in">this</span>.errorHandler);      }        getEmployeeById(id: number) {          <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>._http.get(<span class="hljs-built_in">this</span>.myAppUrl + <span class="hljs-string">"api/Employee/Details/"</span> + id)              .map(<span class="hljs-function">(<span class="hljs-params">response: Response</span>) =&gt;</span> response.json())              .catch(<span class="hljs-built_in">this</span>.errorHandler)      }        saveEmployee(employee) {          <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>._http.post(<span class="hljs-built_in">this</span>.myAppUrl + <span class="hljs-string">'api/Employee/Create'</span>, employee)              .map(<span class="hljs-function">(<span class="hljs-params">response: Response</span>) =&gt;</span> response.json())              .catch(<span class="hljs-built_in">this</span>.errorHandler)      }        updateEmployee(employee) {          <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>._http.put(<span class="hljs-built_in">this</span>.myAppUrl + <span class="hljs-string">'api/Employee/Edit'</span>, employee)              .map(<span class="hljs-function">(<span class="hljs-params">response: Response</span>) =&gt;</span> response.json())              .catch(<span class="hljs-built_in">this</span>.errorHandler);      }        deleteEmployee(id) {          <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>._http.delete(<span class="hljs-built_in">this</span>.myAppUrl + <span class="hljs-string">"api/Employee/Delete/"</span> + id)              .map(<span class="hljs-function">(<span class="hljs-params">response: Response</span>) =&gt;</span> response.json())              .catch(<span class="hljs-built_in">this</span>.errorHandler);      }        errorHandler(error: Response) {          <span class="hljs-built_in">console</span>.log(error);          <span class="hljs-keyword">return</span> Observable.throw(error);      }  }
</code></pre><p>At this point, you might get the following error: “Parameter ‘employee’ implicitly has an ‘any’ type” in “empservice.service.ts” file.</p>
<p>If you encounter this issue, then add the following line inside the “tsconfig.json” file.</p>
<p>“noImplicitAny”: false</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/d7o16eoU-aQFvrJ34miiRbbQTDUGkUZ5cObG" alt="Image" width="303" height="283" loading="lazy"></p>
<p>Now, we will create our components.</p>
<h3 id="heading-creating-angular-components">Creating Angular components</h3>
<p>We will be adding two Angular components to our application:</p>
<ol>
<li><code>fetchemployee</code> component: to display all the employee data or delete existing employee data.</li>
<li><code>addemployee</code> component: to add a new employee data or edit an existing employee data.</li>
</ol>
<p>Right-click on the “ClientApp/app/components” folder and select Add &gt;&gt; New Folder and name the folder “addem_p_loyee”.</p>
<p>Right-click on the “addemployee” folder, and select Add &gt;&gt; New Item. An “Add New Item” dialog box will open.</p>
<p>Select “Scripts<em>”</em> from the left panel, then select “TypeScript File” from templates panel. Put the name as <code>addemployee.component.ts</code>.</p>
<p>Click “Add”. This will add a typescript file inside the “addemployee” folder.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/AcmOw3ktlM2vZn8ifpuABDRetETxUyQ0UrsE" alt="Image" width="650" height="397" loading="lazy"></p>
<p>Right-click on the “addemployee” folder and select Add &gt;&gt; New Item. An “Add New Item” dialog box will open.</p>
<p>Select “ASP.NET Core<em>”</em> from the left panel, then select “HTML Page” from templates panel. Put the name as <code>addemployee.component.html</code><em>.</em></p>
<p>Click “Add”. This will add a HTML file inside the “addemployee” folder.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/k6398O41ak2SjjKxGswS6tb00SvfqIgGtI8q" alt="Image" width="650" height="397" loading="lazy"></p>
<p>Similarly, create a “fetchemployee” folder inside the “ClientApp/app/components” folder.</p>
<p>Add <code>fetchemployee.component.ts</code> and <code>fetchemployee.component.html</code> files to it.</p>
<p>Now our “ClientApp/app/components” structure will look like the image below.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1Ui2XGkgtU7CYRm0Jexoqrz4ZjhI89hYDTul" alt="Image" width="311" height="320" loading="lazy"></p>
<p>Open <code>fetchemployee.component.ts</code> and insert the following code:</p>
<pre><code><span class="hljs-keyword">import</span> { Component, Inject } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>;  <span class="hljs-keyword">import</span> { Http, Headers } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/http'</span>;  <span class="hljs-keyword">import</span> { Router, ActivatedRoute } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/router'</span>;  <span class="hljs-keyword">import</span> { EmployeeService } <span class="hljs-keyword">from</span> <span class="hljs-string">'../../services/empservice.service'</span>    @Component({      <span class="hljs-attr">templateUrl</span>: <span class="hljs-string">'./fetchemployee.component.html'</span>  })    <span class="hljs-keyword">export</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FetchEmployeeComponent</span> </span>{      public empList: EmployeeData[];        <span class="hljs-keyword">constructor</span>(public http: Http, private _router: Router, private _employeeService: EmployeeService) {          <span class="hljs-built_in">this</span>.getEmployees();      }        getEmployees() {          <span class="hljs-built_in">this</span>._employeeService.getEmployees().subscribe(              <span class="hljs-function"><span class="hljs-params">data</span> =&gt;</span> <span class="hljs-built_in">this</span>.empList = data          )      }        <span class="hljs-keyword">delete</span>(employeeID) {          <span class="hljs-keyword">var</span> ans = confirm(<span class="hljs-string">"Do you want to delete customer with Id: "</span> + employeeID);          <span class="hljs-keyword">if</span> (ans) {              <span class="hljs-built_in">this</span>._employeeService.deleteEmployee(employeeID).subscribe(<span class="hljs-function">(<span class="hljs-params">data</span>) =&gt;</span> {                  <span class="hljs-built_in">this</span>.getEmployees();              }, <span class="hljs-function"><span class="hljs-params">error</span> =&gt;</span> <span class="hljs-built_in">console</span>.error(error))          }      }  }    interface EmployeeData {      <span class="hljs-attr">employeeId</span>: number;      name: string;      gender: string;      city: string;      department: string;    }
</code></pre><p>Let’s understand this code.</p>
<p>At the very top we have imported Angular modules and EmployeeService references. After this we use <code>@Component</code> decorator to define the template URL for our component.</p>
<p>Inside the <code>FetchEmployeeComponent</code> class we have declared an array variable <code>empList</code> of type <code>EmployeeData</code><em>.</em> <code>EmployeeData</code> is an interface having the properties same as our <code>TblEmployeeModel</code> class.</p>
<p>Inside the <code>getEmployees</code> method we are calling the <code>getEmployees</code> method of our service <code>EmployeeService</code>. This will return an array of <code>Employees</code> to be stored in the <code>empList</code> variable. The <code>getEmployees</code> method is called inside the constructor, so that the employee data will be displayed as the page loads.</p>
<p>Next, we have a <code>delete</code> method which accepts <code>employeeID</code> as a parameter. This will prompt the user with a confirmation box. If the user selects “yes” then it will delete the employee with this employeeID.</p>
<p>Open <code>fetchemployee.component.html</code> and insert the following code:</p>
<pre><code>&lt;h1&gt;Employee Data&lt;<span class="hljs-regexp">/h1&gt;    &lt;p&gt;This component demonstrates fetching Employee data from the server.&lt;/</span>p&gt;    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span> *<span class="hljs-attr">ngIf</span>=<span class="hljs-string">"!empList"</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">em</span>&gt;</span>Loading...<span class="hljs-tag">&lt;/<span class="hljs-name">em</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>      <span class="hljs-tag">&lt;<span class="hljs-name">a</span> [<span class="hljs-attr">routerLink</span>]=<span class="hljs-string">"['/register-employee']"</span>&gt;</span>Create New<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>  <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span></span>    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">table</span> <span class="hljs-attr">class</span>=<span class="hljs-string">'table'</span> *<span class="hljs-attr">ngIf</span>=<span class="hljs-string">"empList"</span>&gt;</span>      <span class="hljs-tag">&lt;<span class="hljs-name">thead</span>&gt;</span>          <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>              <span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>EmployeeId<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span>              <span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>Name<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span>              <span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>Gender<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span>              <span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>Department<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span>              <span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>City<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span>          <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>      <span class="hljs-tag">&lt;/<span class="hljs-name">thead</span>&gt;</span>      <span class="hljs-tag">&lt;<span class="hljs-name">tbody</span>&gt;</span>          <span class="hljs-tag">&lt;<span class="hljs-name">tr</span> *<span class="hljs-attr">ngFor</span>=<span class="hljs-string">"let emp of empList"</span>&gt;</span>              <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>{{ emp.employeeId }}<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>              <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>{{ emp.name }}<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>              <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>{{ emp.gender }}<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>              <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>{{ emp.department }}<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>              <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>{{ emp.city }}<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>              <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>              <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>                  <span class="hljs-tag">&lt;<span class="hljs-name">a</span> [<span class="hljs-attr">routerLink</span>]=<span class="hljs-string">"['/employee/edit/', emp.employeeId]"</span>&gt;</span>Edit<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span> |                  <span class="hljs-tag">&lt;<span class="hljs-name">a</span> [<span class="hljs-attr">routerLink</span>]=<span class="hljs-string">""</span> (<span class="hljs-attr">click</span>)=<span class="hljs-string">"delete(emp.employeeId)"</span>&gt;</span>Delete<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>              <span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>          <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>      <span class="hljs-tag">&lt;/<span class="hljs-name">tbody</span>&gt;</span>  <span class="hljs-tag">&lt;/<span class="hljs-name">table</span>&gt;</span></span>
</code></pre><p>The code for this HTML file is pretty simple.</p>
<p>At the top it has a link to create new employee record. After that it has a table to display employee data, and two links for editing and deleting each employee record.</p>
<p>We are finished with our <code>fetchemployee</code> component.</p>
<p>Now open <code>addemployee.component.ts</code> and insert the following code.</p>
<pre><code><span class="hljs-keyword">import</span> { Component, OnInit } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>;  <span class="hljs-keyword">import</span> { Http, Headers } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/http'</span>;  <span class="hljs-keyword">import</span> { NgForm, FormBuilder, FormGroup, Validators, FormControl } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/forms'</span>;  <span class="hljs-keyword">import</span> { Router, ActivatedRoute } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/router'</span>;  <span class="hljs-keyword">import</span> { FetchEmployeeComponent } <span class="hljs-keyword">from</span> <span class="hljs-string">'../fetchemployee/fetchemployee.component'</span>;  <span class="hljs-keyword">import</span> { EmployeeService } <span class="hljs-keyword">from</span> <span class="hljs-string">'../../services/empservice.service'</span>;    @Component({      <span class="hljs-attr">templateUrl</span>: <span class="hljs-string">'./AddEmployee.component.html'</span>  })    <span class="hljs-keyword">export</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">createemployee</span> <span class="hljs-title">implements</span> <span class="hljs-title">OnInit</span> </span>{      employeeForm: FormGroup;      title: string = <span class="hljs-string">"Create"</span>;      employeeId: number;      errorMessage: any;      cityList: <span class="hljs-built_in">Array</span>&lt;any&gt; = [];        <span class="hljs-keyword">constructor</span>(private _fb: FormBuilder, private _avRoute: ActivatedRoute,          private _employeeService: EmployeeService, private _router: Router) {          <span class="hljs-keyword">if</span> (<span class="hljs-built_in">this</span>._avRoute.snapshot.params[<span class="hljs-string">"id"</span>]) {              <span class="hljs-built_in">this</span>.employeeId = <span class="hljs-built_in">this</span>._avRoute.snapshot.params[<span class="hljs-string">"id"</span>];          }            <span class="hljs-built_in">this</span>.employeeForm = <span class="hljs-built_in">this</span>._fb.group({              <span class="hljs-attr">employeeId</span>: <span class="hljs-number">0</span>,              <span class="hljs-attr">name</span>: [<span class="hljs-string">''</span>, [Validators.required]],              <span class="hljs-attr">gender</span>: [<span class="hljs-string">''</span>, [Validators.required]],              <span class="hljs-attr">department</span>: [<span class="hljs-string">''</span>, [Validators.required]],              <span class="hljs-attr">city</span>: [<span class="hljs-string">''</span>, [Validators.required]]          })      }        ngOnInit() {            <span class="hljs-built_in">this</span>._employeeService.getCityList().subscribe(              <span class="hljs-function"><span class="hljs-params">data</span> =&gt;</span> <span class="hljs-built_in">this</span>.cityList = data          )            <span class="hljs-keyword">if</span> (<span class="hljs-built_in">this</span>.employeeId &gt; <span class="hljs-number">0</span>) {              <span class="hljs-built_in">this</span>.title = <span class="hljs-string">"Edit"</span>;              <span class="hljs-built_in">this</span>._employeeService.getEmployeeById(<span class="hljs-built_in">this</span>.employeeId)                  .subscribe(<span class="hljs-function"><span class="hljs-params">resp</span> =&gt;</span> <span class="hljs-built_in">this</span>.employeeForm.setValue(resp)                  , <span class="hljs-function"><span class="hljs-params">error</span> =&gt;</span> <span class="hljs-built_in">this</span>.errorMessage = error);          }        }        save() {            <span class="hljs-keyword">if</span> (!<span class="hljs-built_in">this</span>.employeeForm.valid) {              <span class="hljs-keyword">return</span>;          }            <span class="hljs-keyword">if</span> (<span class="hljs-built_in">this</span>.title == <span class="hljs-string">"Create"</span>) {              <span class="hljs-built_in">this</span>._employeeService.saveEmployee(<span class="hljs-built_in">this</span>.employeeForm.value)                  .subscribe(<span class="hljs-function">(<span class="hljs-params">data</span>) =&gt;</span> {                      <span class="hljs-built_in">this</span>._router.navigate([<span class="hljs-string">'/fetch-employee'</span>]);                  }, <span class="hljs-function"><span class="hljs-params">error</span> =&gt;</span> <span class="hljs-built_in">this</span>.errorMessage = error)          }          <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-built_in">this</span>.title == <span class="hljs-string">"Edit"</span>) {              <span class="hljs-built_in">this</span>._employeeService.updateEmployee(<span class="hljs-built_in">this</span>.employeeForm.value)                  .subscribe(<span class="hljs-function">(<span class="hljs-params">data</span>) =&gt;</span> {                      <span class="hljs-built_in">this</span>._router.navigate([<span class="hljs-string">'/fetch-employee'</span>]);                  }, <span class="hljs-function"><span class="hljs-params">error</span> =&gt;</span> <span class="hljs-built_in">this</span>.errorMessage = error)          }      }        cancel() {          <span class="hljs-built_in">this</span>._router.navigate([<span class="hljs-string">'/fetch-employee'</span>]);      }        <span class="hljs-keyword">get</span> <span class="hljs-title">name</span>() { <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.employeeForm.get(<span class="hljs-string">'name'</span>); }      <span class="hljs-keyword">get</span> <span class="hljs-title">gender</span>() { <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.employeeForm.get(<span class="hljs-string">'gender'</span>); }      <span class="hljs-keyword">get</span> <span class="hljs-title">department</span>() { <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.employeeForm.get(<span class="hljs-string">'department'</span>); }      <span class="hljs-keyword">get</span> <span class="hljs-title">city</span>() { <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.employeeForm.get(<span class="hljs-string">'city'</span>); }  }
</code></pre><p>This component will be used for both adding and editing the employee data.</p>
<p>Since we are using a form model, along with client-side validation to add and edit employee data, we have imported classes from @angular/forms. The code to create the form has been put inside the constructor so that the form will be displayed as the page loads.</p>
<p>This component will handle both add and edit requests. So how will the system differentiate between both requests? The answer is routing. We need to define two different route parameters. One is for adding employee records. The other is for editing employee records. These route parameters will be defined in <code>app.shared.module.ts</code> in the following section.</p>
<p>We have declared the variable <code>title</code> to show on the top of the page and variable <code>id</code> to store the employee id passed as the parameter in case of an edit request. To read the employee ID from the URL we will use <code>ActivatedRoute.snapshot</code> inside the constructor, and set the value of variable <code>id</code>.</p>
<p>Inside <code>ngOnInit</code> we are performing two operations:</p>
<ol>
<li>We are fetching the list of cities by calling the <code>getCityList</code> method from our service. We will bind the list of cities to a dropdown list in our HTML page. Since we are calling the <code>getCityList</code> method in <code>ngOnInit</code>, the dropdown list will be populated as the page loads.</li>
<li>We will check if the <code>id</code> is set then we will change the title to “Edit”, get the data for that <code>id</code> from our service, and populate the fields in our form. The value read from the database will be returned as JSON. It will have all the same properties as we declared in our <code>FormBuilder</code>, hence we use the <code>setValue</code> method to populate our form.</li>
</ol>
<p>The save method will be called when the “Save” button of our form is clicked. The <strong>add</strong> and <strong>edit</strong> operations will call the corresponding method from our service and then, upon success, redirect back to the fetch-employee component.</p>
<p>In the last one we have also defined getter functions for the control names of our form to enable client-side validation.</p>
<p>Open <code>addemployee.component.html</code> and insert the following code.</p>
<pre><code>&lt;!DOCTYPE html&gt;  &lt;html&gt;  &lt;head&gt;      &lt;meta charset="utf-8" /&gt;      &lt;title&gt;&lt;/title&gt;  &lt;/head&gt;  &lt;body&gt;        &lt;h1&gt;{{title}}&lt;/h1&gt;      &lt;h3&gt;Employee&lt;/h3&gt;      &lt;hr /&gt;      &lt;form [formGroup]="employeeForm" (ngSubmit)="save()" #formDir="ngForm" novalidate&gt;            &lt;div class="form-group row"&gt;              &lt;label class=" control-label col-md-12" for="Name"&gt;Name&lt;/label&gt;              &lt;div class="col-md-4"&gt;                  &lt;input class="form-control" type="text" formControlName="name"&gt;              &lt;/div&gt;              &lt;span class="text-danger" *ngIf="employeeForm.hasError('required', 'name') &amp;&amp; formDir.submitted"&gt;                  Name is required.              &lt;/span&gt;          &lt;/div&gt;          &lt;div class="form-group row"&gt;              &lt;label class="control-label col-md-12" for="Gender"&gt;Gender&lt;/label&gt;              &lt;div class="col-md-4"&gt;                  &lt;select class="form-control" data-val="true" formControlName="gender"&gt;                      &lt;option value=""&gt;-- Select Gender --&lt;/option&gt;                      &lt;option value="Male"&gt;Male&lt;/option&gt;                      &lt;option value="Female"&gt;Female&lt;/option&gt;                  &lt;/select&gt;              &lt;/div&gt;              &lt;span class="text-danger" *ngIf="employeeForm.hasError('required', 'gender') &amp;&amp; formDir.submitted"&gt;                  Gender is required              &lt;/span&gt;          &lt;/div&gt;          &lt;div class="form-group row"&gt;              &lt;label class="control-label col-md-12" for="Department"&gt;Department&lt;/label&gt;              &lt;div class="col-md-4"&gt;                  &lt;input class="form-control" type="text" formControlName="department"&gt;              &lt;/div&gt;              &lt;span class="text-danger" *ngIf="employeeForm.hasError('required', 'department') &amp;&amp; formDir.submitted"&gt;                  Department is required              &lt;/span&gt;          &lt;/div&gt;          &lt;div class="form-group row"&gt;              &lt;label class="control-label col-md-12" for="City"&gt;City&lt;/label&gt;              &lt;div class="col-md-4"&gt;                  &lt;select class="form-control" data-val="true" formControlName="city"&gt;                      &lt;option value=""&gt;--Select City--&lt;/option&gt;                      &lt;option *ngFor="let city of cityList"                              value={{city.cityName}}&gt;                          {{city.cityName}}                      &lt;/option&gt;                  &lt;/select&gt;              &lt;/div&gt;              &lt;span class="text-danger" *ngIf="employeeForm.hasError('required', 'city') &amp;&amp; formDir.submitted"&gt;                  City is required              &lt;/span&gt;          &lt;/div&gt;          &lt;div class="form-group"&gt;              &lt;button type="submit" class="btn btn-default"&gt;Save&lt;/button&gt;              &lt;button class="btn" (click)="cancel()"&gt;Cancel&lt;/button&gt;          &lt;/div&gt;      &lt;/form&gt;  &lt;/body&gt;  &lt;/html&gt;
</code></pre><p>Here you can observe that we have attribute <code>[formGroup]=“employeeForm”</code>, which is our defined form group name in <code>addemployee.component.ts</code>. <code>(ngSubmit)=“save()”</code> will invoke our <code>save</code> method on form submit.</p>
<p>Also, every input control has attribute <code>formControlName=“xyz”</code>. This is used to bind <code>FormControl</code> to HTML. We have also defined error messages for client-side validation checks. These will be invoked on form submission only.</p>
<p>For binding the dropdown list we are using the <code>cityList</code> property that we have populated from <code>tblCities</code>. It was populated by calling the <code>getCityList</code> method from our service, inside the <code>ngOnInit</code> method of <code>_addemployee.component.ts_</code><em>.</em></p>
<h3 id="heading-defining-the-route-and-navigation-menu-for-our-application">Defining the route and navigation menu for our Application</h3>
<p>Inside the “app” folder, open <code>app.shared.module.ts</code> and insert the following code:</p>
<pre><code><span class="hljs-keyword">import</span> { NgModule } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>;  <span class="hljs-keyword">import</span> { EmployeeService } <span class="hljs-keyword">from</span> <span class="hljs-string">'./services/empservice.service'</span>  <span class="hljs-keyword">import</span> { CommonModule } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/common'</span>;  <span class="hljs-keyword">import</span> { FormsModule, ReactiveFormsModule } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/forms'</span>;  <span class="hljs-keyword">import</span> { HttpModule } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/http'</span>;  <span class="hljs-keyword">import</span> { RouterModule } <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/router'</span>;    <span class="hljs-keyword">import</span> { AppComponent } <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/app/app.component'</span>;  <span class="hljs-keyword">import</span> { NavMenuComponent } <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/navmenu/navmenu.component'</span>;  <span class="hljs-keyword">import</span> { HomeComponent } <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/home/home.component'</span>;  <span class="hljs-keyword">import</span> { FetchEmployeeComponent } <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/fetchemployee/fetchemployee.component'</span>  <span class="hljs-keyword">import</span> { createemployee } <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/addemployee/AddEmployee.component'</span>    @NgModule({      <span class="hljs-attr">declarations</span>: [          AppComponent,          NavMenuComponent,          HomeComponent,          FetchEmployeeComponent,          createemployee,      ],      <span class="hljs-attr">imports</span>: [          CommonModule,          HttpModule,          FormsModule,          ReactiveFormsModule,          RouterModule.forRoot([              { <span class="hljs-attr">path</span>: <span class="hljs-string">''</span>, <span class="hljs-attr">redirectTo</span>: <span class="hljs-string">'home'</span>, <span class="hljs-attr">pathMatch</span>: <span class="hljs-string">'full'</span> },              { <span class="hljs-attr">path</span>: <span class="hljs-string">'home'</span>, <span class="hljs-attr">component</span>: HomeComponent },              { <span class="hljs-attr">path</span>: <span class="hljs-string">'fetch-employee'</span>, <span class="hljs-attr">component</span>: FetchEmployeeComponent },              { <span class="hljs-attr">path</span>: <span class="hljs-string">'register-employee'</span>, <span class="hljs-attr">component</span>: createemployee },              { <span class="hljs-attr">path</span>: <span class="hljs-string">'employee/edit/:id'</span>, <span class="hljs-attr">component</span>: createemployee },              { <span class="hljs-attr">path</span>: <span class="hljs-string">'**'</span>, <span class="hljs-attr">redirectTo</span>: <span class="hljs-string">'home'</span> }          ])      ],      <span class="hljs-attr">providers</span>: [EmployeeService]  })  <span class="hljs-keyword">export</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AppModuleShared</span> </span>{  }
</code></pre><p>Here we have also imported all our components and defined the route for our application as below:</p>
<ul>
<li>home: which will redirect to the <code>home</code> component</li>
<li>fetch-employee: to display all employee data using the <code>fetchemployee</code>component</li>
<li>register-employee: to add new employee record using the <code>createemployee</code>component</li>
<li>employee/edit/:id: to edit existing employee record using the <code>createemployee</code> component</li>
</ul>
<p>One last thing is to the define navigation menu for our application. In “/app/components/navmenu/”, open <code>navmenu.component.html</code> and insert the following code.</p>
<pre><code>&lt;div <span class="hljs-class"><span class="hljs-keyword">class</span></span>=<span class="hljs-string">'main-nav'</span>&gt;      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">'navbar navbar-inverse'</span>&gt;</span>          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">'navbar-header'</span>&gt;</span>              <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">'button'</span> <span class="hljs-attr">class</span>=<span class="hljs-string">'navbar-toggle'</span> <span class="hljs-attr">data-toggle</span>=<span class="hljs-string">'collapse'</span> <span class="hljs-attr">data-target</span>=<span class="hljs-string">'.navbar-collapse'</span>&gt;</span>                  <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">class</span>=<span class="hljs-string">'sr-only'</span>&gt;</span>Toggle navigation<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>                  <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">class</span>=<span class="hljs-string">'icon-bar'</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>                  <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">class</span>=<span class="hljs-string">'icon-bar'</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>                  <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">class</span>=<span class="hljs-string">'icon-bar'</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>              <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>              <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">'navbar-brand'</span> [<span class="hljs-attr">routerLink</span>]=<span class="hljs-string">"['/home']"</span>&gt;</span>ASPCoreWithAngular<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">'clearfix'</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">'navbar-collapse collapse'</span>&gt;</span>              <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">class</span>=<span class="hljs-string">'nav navbar-nav'</span>&gt;</span>                  <span class="hljs-tag">&lt;<span class="hljs-name">li</span> [<span class="hljs-attr">routerLinkActive</span>]=<span class="hljs-string">"['link-active']"</span>&gt;</span>                      <span class="hljs-tag">&lt;<span class="hljs-name">a</span> [<span class="hljs-attr">routerLink</span>]=<span class="hljs-string">"['/home']"</span>&gt;</span>                          <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">class</span>=<span class="hljs-string">'glyphicon glyphicon-home'</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span> Home                      <span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>                  <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>                  <span class="hljs-tag">&lt;<span class="hljs-name">li</span> [<span class="hljs-attr">routerLinkActive</span>]=<span class="hljs-string">"['link-active']"</span>&gt;</span>                      <span class="hljs-tag">&lt;<span class="hljs-name">a</span> [<span class="hljs-attr">routerLink</span>]=<span class="hljs-string">"['/fetch-employee']"</span>&gt;</span>                          <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">class</span>=<span class="hljs-string">'glyphicon glyphicon-th-list'</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span> Fetch employee                      <span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>                  <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>              <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>  &lt;/div&gt;
</code></pre><p>And that’s it. We have created our first ASP.NET Core application using Angular 5 and Entity Framework core database first approach.</p>
<h3 id="heading-execution-demo">Execution Demo</h3>
<p>Press F5 to launch the application.</p>
<p>A web page will open as shown in the image below. Notice the URL showing the route for our home component. The navigation menu on the left shows the navigation link for the “Home” and “Fetch Employee” pages.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/zfm-H3j-jMEE5ZMZQCGdVCUludViC0zJS3kr" alt="Image" width="650" height="330" loading="lazy"></p>
<p>Click on “Fetch Employee” in the navigation menu. It will redirect to the <code>fetchemployee</code> component, and displays all the employee data on the page.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/hbGnwdxmGM8m9P4Va-xrwBK0BKha0dernmQP" alt="Image" width="650" height="181" loading="lazy"></p>
<p>Since we have not added any data it is empty.</p>
<p>Click on “Create New<em>”</em> to navigate to the “/register-employee<em>”</em> page. Add a new Employee record as shown in the image below. You can see that the <strong>City</strong> field is a dropdown list, containing all the city names that we inserted into <code>tblCities</code>.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/iNzUNhKhYjlpyKyc7WlYFBkyeAnNeNvB6BG9" alt="Image" width="563" height="594" loading="lazy"></p>
<p>If we miss the data in any field while creating an employee record, we will get a required field validation error message</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/SYafIcJS18WIjKzPWiqJfpEBt9ay7fmBeGoN" alt="Image" width="640" height="596" loading="lazy"></p>
<p>After inserting the data in all the fields, click on the “Save” button. The new employee record will be created, and you will be redirected to the “/fetch-employee<em>”</em> page. This page displays the records of all the employees. Here, we can also see action methods “Edit” and “Delete”.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/rlUf3JpBh7uj69tQ2Fs9mWA47OKGTSiVxoSt" alt="Image" width="650" height="241" loading="lazy"></p>
<p>If we want to edit an existing employee record, click the “Edit” action link. It will open the “Edit” page as below, where we can change the employee data. Notice that we have passed the EmployeeId in the URL parameter.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/XUU6X3xe7tBTjCrTKRPLNDL7-t7Yaw3E4KFb" alt="Image" width="550" height="625" loading="lazy"></p>
<p>Here we have changed the <code>City</code> of employee Rahul from Hyderabad to Chennai. Click on “Save” to return to the “fetch-employee” page to see the updated changes as highlighted in the image below.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/fYGlFUkpv6LrwAMETcSFnSU-nFeMPNBthFTL" alt="Image" width="650" height="253" loading="lazy"></p>
<p>If we miss any fields while editing the employee records, then Edit view will also throw a required field validation error message as shown in the image below:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/MejmWwE9kxJG2rwT7Abkj69Y6gcU3P6ZFrAy" alt="Image" width="646" height="567" loading="lazy"></p>
<p>Now, we will perform a “Delete” operation on an employee named Swati with Employee Id 2. Click on the “Delete” action link. This will open a JavaScript confirmation box asking for a confirmation to delete.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/4phku3XYBQRTKjYWjzhHNaglb7ZDk80nFkHD" alt="Image" width="650" height="255" loading="lazy"></p>
<p>Once we click on “OK”, the Swati who is Employee Id 2 will be removed from our record. You can see the updated list of employees as shown below.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/8iRuxCuipFk-moKUTwXdIVgHTQ9WgTt91Xga" alt="Image" width="650" height="225" loading="lazy"></p>
<h3 id="heading-other-useful-sources">Other useful sources:</h3>
<ul>
<li><a target="_blank" href="http://ankitsharmablogs.com/crud-operations-asp-net-core-using-angular-5-ado-net/">CRUD Operations With ASP.NET Core Using Angular 5 and ADO.NET</a></li>
<li><a target="_blank" href="http://ankitsharmablogs.com/getting-started-with-angular-5-using-visual-studio-code/">Getting Started With Angular 5 Using Visual Studio Code</a></li>
<li><a target="_blank" href="http://ankitsharmablogs.com/crud-operation-asp-net-core-mvc-using-visual-studio-code-ef/">CRUD Operation With ASP.NET Core MVC Using Visual Studio Code and EF</a></li>
<li><a target="_blank" href="http://ankitsharmablogs.com/crud-operation-with-asp-net-core-mvc-using-ado-net/">CRUD Operation With ASP.NET Core MVC Using ADO.NET and Visual Studio 2017</a></li>
<li><a target="_blank" href="http://ankitsharmablogs.com/crud-operation-with-asp-net-core-mvc-using-visual-studio-code-and-ado-net/">CRUD Operation With ASP.NET Core MVC using Visual Studio Code and ADO.NET</a></li>
<li><a target="_blank" href="http://ankitsharmablogs.com/asp-net-core-getting-started-with-blazor/">ASP.NET Core — Getting Started With Blazor</a></li>
</ul>
<h3 id="heading-conclusion">Conclusion</h3>
<p>We have successfully created an ASP.NET Core application using Angular 5 and Entity Framework core database first approach with the help of Visual Studio 2017 and SQL Server 2012. We have used Angular forms to get data from the user and also bind the dropdown list to the database table using Entity framework.</p>
<p>You can check out my other articles on ASP .NET Core <a target="_blank" href="http://ankitsharmablogs.com/category/asp-net-core/">here</a>.</p>
<p>Originally published at <a target="_blank" href="https://ankitsharmablogs.com/">https://ankitsharmablogs.com/</a></p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
