Thursday, August 22, 2019

Entity Framework Core Select all rows from a Table

Selecting all the rows from a table is very simple, assuming that we have setup the Entity Framework Core context and generated all the Models we can use the context object to select all the rows from the Employee tables as follows.

List_context.Employee.ToList();


If we want to get all the Employees in a MVC Controller then the controller will be as follows

        public IActionResult Index()
        {
            List<Employee> Employees = _context.Employee.ToList();
            return View(Employees);
        }

And the View to display the employees will be as follows.

@model IEnumerable
@{
    ViewData["Title"] = "Index";
}
<div class="container">
    <div class="row">
        <div class="col-md-2">
            <h4>Employee List</h4>
        </div>
        <div class="col-md-2">
            <a asp-action="Create">Create New</a>
        </div>
        <div class="col-md-2">
            <button type="button" class="btn btn-sm btn-primary" data-target="#modalHelp" data-toggle="modal">Help</button>
        </div>
    </div>
        <div class="row">
            <div class="col-md-2">Id</div>
            <div class="col-md-2">Name</div>
            <div class="col-md-2">Age</div>
            <div class="col-md-2">Phone</div>
            <div class="col-md-2">Actions</div>
        </div>
        @foreach (var item in Model)
        {
            <div class="row">
                <div class="col-md-2">@Html.DisplayFor(modelItem => item.EmployeeId)</div>
                <div class="col-md-2">@Html.DisplayFor(modelItem => item.Name)</div>
                <div class="col-md-2">@Html.DisplayFor(modelItem => item.Age)</div>
                <div class="col-md-2">@Html.DisplayFor(modelItem => item.Phone)</div>
                <div class="col-md-2">
                    <a asp-action="Edit" asp-route-id="@item.EmployeeId">Edit</a> |
                    @Html.ActionLink("Delete", "Delete", new { id = item.EmployeeId },
                        new { onclick = "return confirm('Are sure wants to delete?');" })
                </div>
            </div>
        }
</div>


Search Flipkart Products:
Flipkart.com

No comments: