In the Previous post
01 - Starting with Asp.Net Core using Visual Studio
I used Visual Studio 2015 Update 3. But unfortunately it is not working as Expected, so from this post I will use Visual Studio 2017.
Out Put:
Create a New Asp.Net Core 1.1 Project using Visual Studio 2017.
Add the Following Packages using NuGet Package Manager.
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.SqlServer.Design
Microsoft.EntityFrameworkCore.Tools
ADD Models Folder in the Project. Now Select Connection String from Sql Server Object Explorer.
Use Scaffold-DbContext to generate Model Classes from Existing DataBase.
In -OutputDir is used as Out Put Directory to place the Generated Model classes
The Entire string looks as shown
Scaffold-DbContext "Data Source=ADMINDESK\MYDATABASE;Initial Catalog=EmployeeDB;Integrated Security=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
Paste the above string in Package Manager Console and press Enter to generate the Models.
Ones they are generated, Replace the Index IActionResult from HomeController.cs file.
public IActionResult Index()
{
using (EmployeeDBContext ent = new EmployeeDBContext())
{
IEnumerable emps = ent.Employees.ToList();
return View(emps);
}
}
Now open Index.cshtml file and replace with the below Code.
@model IEnumerable<WebApplication1.Models.Employees>
@{
ViewData["Title"] = "Home Page";
}
<div class="row">
<br />
@Html.ActionLink("ADD Employee", "ADD", null, null,
new { data_modal = "", id = "btnADD", @class = "btn btn-primary" })
<div style="max-height:350px; overflow-y:auto">
<table class="table table-hover table-bordered">
<thead>
<tr>
@*@Html.DisplayNameFor(model => model.)*@
<th>First Name</th>
<th>Last Name</th>
<th>Gender</th>
<th>Salary</th>
<th>DOJ</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(itemModel => item.FirstName)
</td>
<td>
@Html.DisplayFor(itemModel => item.LastName)
</td>
<td>
@Html.DisplayFor(itemModel => item.Gender)
</td>
<td>
@Html.DisplayFor(itemModel => item.Salary)
</td>
<td>
@item.Doj.ToString("dd-MM-yyyy")
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
Now Press F5 to see the output.
No comments:
Post a Comment