CRUD Operation - ASP.NET Core MVC with EF Core

CRUD Operation - ASP.NET Core MVC with EF Core
ASP.NET Core MVC with EF Core CRUD Operation Source Code Free Download
Source Code of this project - just click on, above ⇪ download button and get it.


CRUD (Create, Read, Update and Delete) Operation

In computer programming the CRUD operation, meaning is Create, Read, Update, and Delete are the four basic functions of persistent storage.

In Structured Query Language (SQL)  

INSERT meaning is "Create",
SELECT meaning is "Read / Retrieve",
UPDATE meaning is "Update / Modify", and
DELETE meaning is "Delete / Destroy".

In Hypertext Transfer Protocol (HTTP) 

PUT / POST meaning is "Create",
GET meaning is "Read / Retrieve",
PUT / POST / PATCH meaning is "Update / Modify", and
DELETE meaning is "Delete / Destroy".

In RESTful web services [Representational State Transfer (REST)]

POST meaning is "Create",
GET meaning is "Read / Retrieve",
PUT meaning is "Update / Modify", and
DELETE meaning is "Delete / Destroy".

Solution Explorer > Solution Name > Project Name

Solution Explorer > Solution Name > Project Name

Solution Explorer > Solution Name > Project Name > Resources


Solution Explorer > Solution Name > Project Name > Resources

Solution Explorer > Solution Name > Project Name > Connected Services

Solution Explorer > Solution Name > Project Name > Connected Services

Solution Explorer > Solution Name > Project Name > Dependencies

Solution Explorer > Solution Name > Project Name > Dependencies

Solution Explorer > Solution Name > Project Name > Dependencies > Analyzers


Solution Explorer > Solution Name > Project Name > Dependencies > Analyzers

Solution Explorer > Solution Name > Project Name > Dependencies > NuGet


Solution Explorer > Solution Name > Project Name > Dependencies > NuGet

Solution Explorer > Solution Name > Project Name > Dependencies > SDK

Solution Explorer > Solution Name > Project Name > Dependencies > SDK

Solution Explorer > Solution Name > Project Name > Properties


Solution Explorer > Solution Name > Project Name > Properties

Solution Explorer > Solution Name > Project Name > wwwroot

Solution Explorer > Solution Name > Project Name > wwwroot

Solution Explorer > Solution Name > Project Name > Controllers


Solution Explorer > Solution Name > Project Name > Migrations

Solution Explorer > Solution Name > Project Name > Migrations

Solution Explorer > Solution Name > Project Name > Models

Solution Explorer > Solution Name > Project Name > Models

Solution Explorer > Solution Name > Project Name > Views

Solution Explorer > Solution Name > Project Name > Views

Solution Explorer > Solution Name > Project Name > appsettings

Solution Explorer > Solution Name > Project Name > appsettings

Solution Explorer > Solution Name > Project Name > Program

Solution Explorer > Solution Name > Project Name > Program

Solution Explorer > Solution Name > Project Name > Startup

Solution Explorer > Solution Name > Project Name > Startup


Start your coding from here:

Step 01:
Install Essential NuGet Packages


> Microsoft.EntityFrameworkCore

> Microsoft.EntityFrameworkCore.SqlServer

> Microsoft.EntityFrameworkCore.Tools

NuGet Packages in Microsoft Visual Studio ⬎
NuGet Packages in Microsoft Visual Studio
Figure: Essential NuGet Packages









Step 02:

📂 Models

▶ Employee.cs

Create Employee.cs class in the Models folder and write the following codes ⬎


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication1.Models
{
    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

Employee.cs class Code in Microsoft Visual Studio ⬎
Employee.cs class Code in Microsoft Visual Studio
Figure: Employee class


























Step 03:

📂 Models

▶ DatabaseContext.cs

Create DatabaseContext.cs class in the Models folder and write the following codes ⬎

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication1.Models
{
    public class DatabaseContext : DbContext
    {
        public DbSet<Employee> Employee { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(@"Data Source=RJ\RJ; initial catalog=RJUniverse; user id=sa; password=rj123;");
        }
    }
}

DatabaseContext.cs class Code in Microsoft Visual Studio ⬎
Figure: DatabaseContext class












Step 04:

Add Migration in to the project, follow the following instruction ⬎ 

Open Package Manager Console

Write add-migration initial command and hit enter button

Use add-migration command in Microsoft Visual Studio ⬎
Use add-migration command in Microsoft Visual Studio
Figure: add-migration initial



Step 05:

Create Database for this project, follow the following instruction ⬎

Open Package Manager Console

Write update-database or update-database -verbose command and hit enter button

Use update-database command in Microsoft Visual Studio ⬎
Use update-database command in Microsoft Visual Studio
Figure: update-database


Or

Use update-database -verbose command in Microsoft Visual Studio ⬎

Use update-database -verbose command in Microsoft Visual Studio
Figure: update-database -verbose



Step 06:

📂 Controllers

▶ EmployeeController.cs

Create EmployeeController.cs controller in the Controllers folder and write the following codes ⬎


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class EmployeeController : Controller
    {
        // Start code for this project - ~RJ~

        DatabaseContext db = new DatabaseContext();


        // Start code for Index page - ~RJ~

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

        // End code for Index page - ~RJ~


        // Start code for Create page - ~RJ~
        public IActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Create(Employee model)
        {
            db.Employee.Add(model);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        // End code for Create page - ~RJ~


        // Start code for Edit and Update Action - ~RJ~
        public IActionResult Update(int Id)
        {
            Employee model = db.Employee.Find(Id);
            return View("Create", model);
        }

        [HttpPost]
        public IActionResult Update(Employee model)
        {
            db.Employee.Update(model);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        // End code for Edit and Update Action - ~RJ~


        // Start code for Delete Action - ~RJ~

        public IActionResult Delete(int Id)
        {
            Employee RJ = db.Employee.Find(Id);
            if (RJ != null)
            {
                db.Employee.Remove(RJ);
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }

        // End code for Delete Action - ~RJ~
    }
}

EmployeeController.cs controller Code in Microsoft Visual Studio ⬎
Figure: EmployeeController


Step 07:

📂 Views

🗁 Employee

▷ Index.cshtml

Create Index.cshtml cshtml file in the Views > Employee folder and write the following codes ⬎

@model IEnumerable<WebApplication1.Models.Employee>

@{
    ViewData["Title"] = "Index";
}

<h2>Index</h2>

<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            <a asp-action="Update" asp-route-id="@item.Id">Edit</a> |
            <a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
        </td>
    </tr>
}
    </tbody>
</table>


Index.cshtml file Code in Microsoft Visual Studio ⬎
Index.cshtml file Code in Microsoft Visual Studio
Figure: Index.cshtml


Step 08:

📂 Views

🗁 Employee

▷ Create.cshtml

Create Create.cshtml cshtml file in the Views > Employee folder and write the following codes ⬎

@model WebApplication1.Models.Employee

@{
    ViewData["Title"] = Model != null ? "Update" : "Create";
}

<h2>Create</h2>

<h4>Employee</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="@ViewData["Title"]">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Name" class="control-label"></label>
                <input asp-for="Name" class="form-control" />
                <span asp-validation-for="Name" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="@ViewData["Title"]" class="btn btn-default" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

Create.cshtml cshtml file Code in Microsoft Visual Studio ⬎
Create.cshtml cshtml file Code in Microsoft Visual Studio
Figure: Create.cshtml


Output:

Figure: Employee Index Window

Figure: Employee Create Window

Figure: After Employee Creation Window

Figure: Edit Window

Figure: After Edit Window

Figure: After Delete Window





Essential NuGet packages for this project

Microsoft.EntityFrameworkCore

Entity Framework Core is a lightweight and extensible version of the popular Entity Framework data access technology.

Commonly Used Types:

Microsoft.EntityFrameworkCore.DbContext
Microsoft.EntityFrameworkCore.DbSet

To install this package by the NuGet Package Manager Console in Visual Studio, use the following command:

PM> Install-Package Microsoft.EntityFrameworkCore -Version 2.1.1


Microsoft.EntityFrameworkCore.SqlServer

Microsoft SQL Server database provider for Entity Framework Core.

To install this package by the NuGet Package Manager Console in Visual Studio, use the following command:

PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 2.1.1


Microsoft.EntityFrameworkCore.Tools

Entity Framework Core Tools for the NuGet Package Manager Console in Visual Studio.

Enables these commonly used commands:

Add-Migration
Drop-Database
Get-DbContext
Scaffold-DbContext
Script-Migrations
Update-Database

To install this package by the NuGet Package Manager Console in Visual Studio, use the following command:

PM> Install-Package Microsoft.EntityFrameworkCore.Tools -Version 2.1.1


Essential Package Manager Console Commands

PM> add-migration initial [This command is used to add Migration into the current project. You have to choose any name to add Migration. Such as "initial".]

PM> update-database [This command is used to Create Database for the current project.]

PM> update-database -verbose [This command is used to Create Database and show the creating process of database for the current project.]

PM> add-migration update1 [This command is used to update Migration into the current project. You have to choose any name for every update. Such as "update1".]

PM> Remove-Migration [This command is used to Remove Migration.]

PM> drop-database [This command is used to remove current database, which is associated with this project.]



Moral: 
Slow and steady wins the race.

Comments

  1. Its very informative blog and useful article thank you for sharing with us , keep posting learn
    .NET Online Course

    ReplyDelete

Post a Comment

Popular posts from this blog

Insert data into a database table using ASP.Net, C# and Microsoft SQL Server Stored Procedure

Forward to 404 page for all missing URL - ASP.NET