avatarCan Sener

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

11759

Abstract

ljs-keyword">if</span> (!ProductExists(product.Id)) { <span class="hljs-keyword">return</span> NotFound(); } <span class="hljs-keyword">else</span> { <span class="hljs-keyword">throw</span>; } } <span class="hljs-keyword">return</span> RedirectToAction(<span class="hljs-keyword">nameof</span>(Index)); } <span class="hljs-keyword">return</span> View(product); }

    [<span class="hljs-meta">HttpPost</span>]
    [<span class="hljs-meta">ValidateAntiForgeryToken</span>]
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">async</span> Task&lt;IActionResult&gt; <span class="hljs-title">Delete</span>(<span class="hljs-params"><span class="hljs-built_in">int</span> id</span>)</span>
    {
        <span class="hljs-keyword">var</span> product = <span class="hljs-keyword">await</span> _context.Products.FindAsync(id);
        <span class="hljs-keyword">if</span> (product == <span class="hljs-literal">null</span>)
        {
            <span class="hljs-keyword">return</span> NotFound();
        }

        _context.Products.Remove(product);
        <span class="hljs-keyword">await</span> _context.SaveChangesAsync();
        <span class="hljs-keyword">return</span> RedirectToAction(<span class="hljs-keyword">nameof</span>(Index));
    }

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-built_in">bool</span> <span class="hljs-title">ProductExists</span>(<span class="hljs-params"><span class="hljs-built_in">int</span> id</span>)</span>
    {
        <span class="hljs-keyword">return</span> _context.Products.Any(e =&gt; e.Id == id);
    }
}

}</pre></div><h1 id="35ee">Step 9: Create Views</h1><p id="0dd8">Create views for listing, creating, editing, and deleting products in the <code>Views/Product</code> directory.</p><h2 id="93a2">Index.cshtml</h2><div id="173c"><pre>@model List<span class="hljs-tag"><<span class="hljs-name">Product</span>></span>

<span class="hljs-tag"><<span class="hljs-name">h1</span>></span>Product List<span class="hljs-tag"></<span class="hljs-name">h1</span>></span>

<span class="hljs-tag"><<span class="hljs-name">p</span>></span> <span class="hljs-tag"><<span class="hljs-name">a</span> <span class="hljs-attr">asp-action</span>=<span class="hljs-string">"Create"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn btn-primary"</span>></span>Create New<span class="hljs-tag"></<span class="hljs-name">a</span>></span> <span class="hljs-tag"></<span class="hljs-name">p</span>></span>

<span class="hljs-tag"><<span class="hljs-name">table</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"table table-striped"</span>></span> <span class="hljs-tag"><<span class="hljs-name">thead</span>></span> <span class="hljs-tag"><<span class="hljs-name">tr</span>></span> <span class="hljs-tag"><<span class="hljs-name">th</span>></span> @Html.DisplayNameFor(model => model.First().Name) <span class="hljs-tag"></<span class="hljs-name">th</span>></span> <span class="hljs-tag"><<span class="hljs-name">th</span>></span> @Html.DisplayNameFor(model => model.First().Price) <span class="hljs-tag"></<span class="hljs-name">th</span>></span> <span class="hljs-tag"><<span class="hljs-name">th</span>></span>Actions<span class="hljs-tag"></<span class="hljs-name">th</span>></span> <span class="hljs-tag"></<span class="hljs-name">tr</span>></span> <span class="hljs-tag"></<span class="hljs-name">thead</span>></span> <span class="hljs-tag"><<span class="hljs-name">tbody</span>></span> @foreach (var product in Model) { <span class="hljs-tag"><<span class="hljs-name">tr</span>></span> <span class="hljs-tag"><<span class="hljs-name">td</span>></span> @Html.DisplayFor(modelItem => product.Name) <span class="hljs-tag"></<span class="hljs-name">td</span>></span> <span class="hljs-tag"><<span class="hljs-name">td</span>></span> @Html.DisplayFor(modelItem => product.Price) <span class="hljs-tag"></<span class="hljs-name">td</span>></span> <span class="hljs-tag"><<span class="hljs-name">td</span>></span> <span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn-group"</span> <span class="hljs-attr">role</span>=<span class="hljs-string">"group"</span>></span> <span class="hljs-tag"><<span class="hljs-name">a</span> <span class="hljs-attr">asp-action</span>=<span class="hljs-string">"Edit"</span> <span class="hljs-attr">asp-route-id</span>=<span class="hljs-string">"@product.Id"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn btn-warning"</span>></span>Edit<span class="hljs-tag"></<span class="hljs-name">a</span>></span> <span class="hljs-tag"><<span class="hljs-name">form</span> <span class="hljs-attr">asp-action</span>=<span class="hljs-string">"Delete"</span> <span class="hljs-attr">asp-route-id</span>=<span class="hljs-string">"@product.Id"</span> <span class="hljs-attr">method</span>=<span class="hljs-string">"post"</span> <span class="hljs-attr">onsubmit</span>=<span class="hljs-string">"return confirm('Are you sure you want to delete this product?')"</span>></span> @Html.AntiForgeryToken() <span class="hljs-tag"><<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn btn-danger"</span>></span>Delete<span class="hljs-tag"></<span class="hljs-name">button</span>></span> <span class="hljs-tag"></<span class="hljs-name">form</span>></span> <span class="hljs-tag"></<span class="hljs-name">div</span>></span> <span class="hljs-tag"></<span class="hljs-name">td</span>></span> <span class="hljs-tag"></<span class="hljs-name">tr</span>></span> } <span class="hljs-tag"></<span class="hljs-name">tbody</span>></span> <span class="hljs-tag"></<span class="hljs-name">table</span>></span></pre></div><h2 id="7fca">Create.cshtml</h2><div id="2df6"><pre>@model Product

<span class="hljs-tag"><<span class="hljs-name">h1</span>></span>Create Product<span class="hljs-tag"></<span class="hljs-name">h1</span>></span>

<span class="hljs-tag"><<span class="hljs-name">div</span>></span> <span class="hljs-tag"><<span class="hljs-name">form</span> <span class="hljs-attr">asp-action</span>=<span class="hljs-string">"Create"</span> <span class="hljs-attr">method</span>=<span class="hljs-string">"post"</span>></span> @await Html.PartialAsync("_ProductForm", Model) <span class="hljs-tag"><<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn btn-primary"</span>></span>Save Changes<span class="hljs-tag"></<span class="hljs-name">button</span>></span> <span class="hljs-tag"></<span class="hljs-name">form</span>></span> <span class="hljs-tag"></<span class="hljs-name">div</span>></span></pre></div><h2 id="4cba">Edit.cshtml</h2><div id="99d0"><pre>@model Product

<span class="hljs-tag"><<span class="hljs-name">h1</span>></span>Edit Product<span class="hljs-tag"></<span class="hljs-name">h1</span>></span>

<span class="hljs-tag"><<span class="hljs-name">div</span>></span> <span class="hljs-tag"><<span class="hljs-name">form</span> <span class="hljs-attr">asp-action</span>=<span class="hljs-string">"Edit"</span> <span class="hljs-attr">method</span>=<span class="hljs-string">"post"</span>></span> @await Html.PartialAsync("_ProductForm", Model) <span class="hljs-tag"><<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn btn-primary"</span>></span>Save Changes<span class="hljs-tag"></<span class="hljs-name">button</span>></span> <span class="hljs-tag"></<span class="hljs-name">form</span>></span> <span class="hljs-tag"></<span class="hljs-name">div</span>></span></pre></div><h1 id="956e">_ProductForm.cshtml (Partial View)</h1><div id="5839"><pre>@model Product

<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"form-group"</span>></span> <span class="hljs-tag"><<span class="hljs-name">label</span> <span class="hljs-attr">asp-for</span>=<span class="hljs-string">"Name"</span>></span><span class="hljs-tag"></<span class="hljs-name">label</span>></span> <span class="hljs-tag"><<span class="hljs-name">input</span> <span class="hljs-attr">asp-for</span>=<span class="hljs-string">"Name"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"form-control"</span> /></span> <span class="hljs-tag"><<span class="hljs-name">span</span> <span class="hljs-attr">asp-validation-for</span>=<span class="hljs-string">"Name"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text-danger"</span>></span><span class="hljs-tag"></<span class="hljs-name">span</span>></span> <span class="hljs-tag"></<span class="hljs-name">div</span>></span>

<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"form-group"</span>></span> <span class="hljs-tag"><<span class="hljs-name">label</span> <span class="hljs-attr">asp-for</span>=<span class="hljs-string">"Price"</span>></span><span class="hljs-tag"></<span class="hljs-name">label</span>></span> <span class="hljs-tag"><<span class="hljs-name">input</span> <span class="hljs-attr">asp-for</span>=<span class="hljs-string">"Price"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"form-control"</span> /></span> <span class="hljs-tag"><<span class="hljs-name">span</span> <span class="hljs-attr">asp-validation-for</span>=<span class="hljs-string">"Price"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text-danger"</span>></span><span class="hljs-tag"></<span class="hljs-name">span</span>></span> <span class="hljs-tag"></<span class="hljs-name">div</span>></span>

<span class="hljs-tag"><<span class="hljs-name">a</span> <span class="hljs-attr">asp-action</span>=<span class="hljs-string">"Index"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn btn-secondary"</span>></span>Back to List<span class="hljs-tag"></<span class="hljs-name">a</span>></span></pre></div><h1 id="29da">Step 9: Layout</h1><p id="73ca">Our layout appears to be like this for proper styling :</p><h2 id="03a7">_Layout.cshtml</h2><div id="a795"><pr

Options

e><span class="hljs-meta"><!DOCTYPE <span class="hljs-keyword">html</span>></span> <span class="hljs-tag"><<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>></span> <span class="hljs-tag"><<span class="hljs-name">head</span>></span> <span class="hljs-tag"><<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"utf-8"</span> /></span> <span class="hljs-tag"><<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span> /></span> <span class="hljs-tag"><<span class="hljs-name">title</span>></span>@ViewData["Title"] - CodeFirstCRUD<span class="hljs-tag"></<span class="hljs-name">title</span>></span> <span class="hljs-tag"><<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/lib/bootstrap/dist/css/bootstrap.min.css"</span> /></span> <span class="hljs-tag"><<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/css/site.css"</span> <span class="hljs-attr">asp-append-version</span>=<span class="hljs-string">"true"</span> /></span> <span class="hljs-tag"><<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"~/CodeFirstCRUD.styles.css"</span> <span class="hljs-attr">asp-append-version</span>=<span class="hljs-string">"true"</span> /></span> <span class="hljs-tag"><<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"</span> <span class="hljs-attr">integrity</span>=<span class="hljs-string">"sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8sh+Wy9HgQCF+6pB1Z+kmokEl4kgp5EqTqFZI"</span> <span class="hljs-attr">crossorigin</span>=<span class="hljs-string">"anonymous"</span>></span> <span class="hljs-tag"></<span class="hljs-name">head</span>></span> <span class="hljs-tag"><<span class="hljs-name">body</span>></span> <span class="hljs-tag"><<span class="hljs-name">header</span>></span> <span class="hljs-tag"><<span class="hljs-name">nav</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3"</span>></span> <span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container-fluid"</span>></span> <span class="hljs-tag"><<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"navbar-brand"</span> <span class="hljs-attr">asp-area</span>=<span class="hljs-string">""</span> <span class="hljs-attr">asp-controller</span>=<span class="hljs-string">"Home"</span> <span class="hljs-attr">asp-action</span>=<span class="hljs-string">"Index"</span>></span>CodeFirstCRUD<span class="hljs-tag"></<span class="hljs-name">a</span>></span> <span class="hljs-tag"><<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"navbar-toggler"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"button"</span> <span class="hljs-attr">data-bs-toggle</span>=<span class="hljs-string">"collapse"</span> <span class="hljs-attr">data-bs-target</span>=<span class="hljs-string">".navbar-collapse"</span> <span class="hljs-attr">aria-controls</span>=<span class="hljs-string">"navbarSupportedContent"</span> <span class="hljs-attr">aria-expanded</span>=<span class="hljs-string">"false"</span> <span class="hljs-attr">aria-label</span>=<span class="hljs-string">"Toggle navigation"</span>></span> <span class="hljs-tag"><<span class="hljs-name">span</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"navbar-toggler-icon"</span>></span><span class="hljs-tag"></<span class="hljs-name">span</span>></span> <span class="hljs-tag"></<span class="hljs-name">button</span>></span> <span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"navbar-collapse collapse d-sm-inline-flex justify-content-between"</span>></span> <span class="hljs-tag"><<span class="hljs-name">ul</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"navbar-nav flex-grow-1"</span>></span> <span class="hljs-tag"><<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"nav-item"</span>></span> <span class="hljs-tag"><<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"nav-link text-dark"</span> <span class="hljs-attr">asp-area</span>=<span class="hljs-string">""</span> <span class="hljs-attr">asp-controller</span>=<span class="hljs-string">"Home"</span> <span class="hljs-attr">asp-action</span>=<span class="hljs-string">"Index"</span>></span>Home<span class="hljs-tag"></<span class="hljs-name">a</span>></span> <span class="hljs-tag"></<span class="hljs-name">li</span>></span> <span class="hljs-tag"><<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"nav-item"</span>></span> <span class="hljs-tag"><<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"nav-link text-dark"</span> <span class="hljs-attr">asp-area</span>=<span class="hljs-string">""</span> <span class="hljs-attr">asp-controller</span>=<span class="hljs-string">"Home"</span> <span class="hljs-attr">asp-action</span>=<span class="hljs-string">"Privacy"</span>></span>Privacy<span class="hljs-tag"></<span class="hljs-name">a</span>></span> <span class="hljs-tag"></<span class="hljs-name">li</span>></span> <span class="hljs-tag"></<span class="hljs-name">ul</span>></span> <span class="hljs-tag"></<span class="hljs-name">div</span>></span> <span class="hljs-tag"></<span class="hljs-name">div</span>></span> <span class="hljs-tag"></<span class="hljs-name">nav</span>></span> <span class="hljs-tag"></<span class="hljs-name">header</span>></span> <span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>></span> <span class="hljs-tag"><<span class="hljs-name">main</span> <span class="hljs-attr">role</span>=<span class="hljs-string">"main"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"pb-3"</span>></span> @RenderBody() <span class="hljs-tag"></<span class="hljs-name">main</span>></span> <span class="hljs-tag"></<span class="hljs-name">div</span>></span>

<span class="hljs-tag">&lt;<span class="hljs-name">footer</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"border-top footer text-muted"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
        <span class="hljs-symbol">&amp;copy;</span> 2024 - CodeFirstCRUD - <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">asp-area</span>=<span class="hljs-string">""</span> <span class="hljs-attr">asp-controller</span>=<span class="hljs-string">"Home"</span> <span class="hljs-attr">asp-action</span>=<span class="hljs-string">"Privacy"</span>&gt;</span>Privacy<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">footer</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"~/lib/jquery/dist/jquery.min.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"~/js/site.js"</span> <span class="hljs-attr">asp-append-version</span>=<span class="hljs-string">"true"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://code.jquery.com/jquery-3.5.1.slim.min.js"</span> <span class="hljs-attr">integrity</span>=<span class="hljs-string">"sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"</span> <span class="hljs-attr">crossorigin</span>=<span class="hljs-string">"anonymous"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"</span> <span class="hljs-attr">integrity</span>=<span class="hljs-string">"sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8sh+Wy9HgQCF+6pB1Z+kmokEl4kgp5EqTqFZI"</span> <span class="hljs-attr">crossorigin</span>=<span class="hljs-string">"anonymous"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
@await RenderSectionAsync("Scripts", required: false)

<span class="hljs-tag"></<span class="hljs-name">body</span>></span> <span class="hljs-tag"></<span class="hljs-name">html</span>></span></pre></div><p id="3ec6">P.S</p><p id="2555">Please let me know if there is anything that seems “broken” in the article.</p><p id="a878">You can also download the source code from: <a href="https://github.com/mcansener/CodeFirstCRUD">https://github.com/mcansener/CodeFirstCRUD</a></p><h1 id="4817">Stackademic</h1><p id="73ba">Thank you for reading until the end. Before you go:</p><ul><li>Please consider <b>clapping</b> and <b>following</b> the writer! 👏</li><li>Follow us <a href="https://twitter.com/stackademichq"><b>X</b></a><b> | <a href="https://www.linkedin.com/company/stackademic">LinkedIn</a> | <a href="https://www.youtube.com/c/stackademic">YouTube</a> | <a href="https://discord.gg/in-plain-english-709094664682340443">Discord</a></b></li><li>Visit our other platforms: <a href="https://plainenglish.io"><b>In Plain English</b></a><b> | <a href="https://cofeed.app/">CoFeed</a> | <a href="https://venturemagazine.net/">Venture</a></b></li></ul></article></body>

Web Application with .NET Core and Entity Framework Code-First CRUD Operations

In this tutorial, we’ll walk through the process of creating a basic CRUD (Create, Read, Update, Delete) web application using .NET Core and Entity Framework with a Code-First approach. We’ll cover setting up the project, creating models, implementing the database context, and building views for listing, creating, editing, and deleting products.

You can also download the source code from: https://github.com/mcansener/CodeFirstCRUD

Prerequisites

Before getting started, ensure that you have the following installed:

  • .NET Core SDK
  • A code editor of your choice (e.g., Visual Studio Code, Visual Studio)

Step 1: Create a new .NET Core Web Application

Open a terminal and run the following command to create a new .NET Core web application:

dotnet new web -n CodeFirstCRUD

Change into the project directory:

cd CodeFirstCRUD

Step 2: Install Entity Framework

Install the Entity Framework tools using the following command:

dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer

Step 3: Create Models

Create a Product model in the Models directory:

using System.ComponentModel.DataAnnotations;

namespace CodeFirstCRUD.Models
{
    public class Product
    {
        [Key]
        public int Id { get; set; }

        [Required]
        public string Name { get; set; }

        [Required]
        [DataType(DataType.Currency)]
        public decimal Price { get; set; }
    }
}

Step 4: Implement Database Context

Create a ProductContext class in the Data directory to define the database context:

using CodeFirstCRUD.Models;
using Microsoft.EntityFrameworkCore;

namespace CodeFirstCRUD.Context
{
    public class AppDbContext : DbContext
    {
        public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }

        public DbSet<Product> Products { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Product>()
                .Property(p => p.Price)
                .HasColumnType("decimal(18, 2)");

            base.OnModelCreating(modelBuilder);
        }
    }
}

Step 5: Configure Connection String

In appsettings.json, configure the connection string for your database:

  "ConnectionStrings": {
    "DefaultConnection": "Server=(local);Database=CodeFirstCRUDDb;Trusted_Connection=True;MultipleActiveResultSets=true;TrustServerCertificate=True;"
  },

Step 6: Register Database Context in Program.cs

In Program.cs, register the database context in the ConfigureServices method:

using CodeFirstCRUD.Context;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

// Configure the database context and migrations
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<AppDbContext>(options =>
    options.UseSqlServer(connectionString));

// Build the application
var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

// Migrate the database
using (var serviceScope = app.Services.CreateScope())
{
    var context = serviceScope.ServiceProvider.GetRequiredService<AppDbContext>();
    context.Database.Migrate();
}

app.Run();

Step 7: Create Database Migrations

Run the following command to create database migrations:

dotnet ef migrations add InitialCreate

Apply the migrations to create the database:

dotnet ef database update

Step 8: Create Controller

Create the related controller for actions :

using CodeFirstCRUD.Context;
using CodeFirstCRUD.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace CodeFirstCRUD.Controllers
{
    public class ProductController : Controller
    {
        private readonly AppDbContext _context;

        public ProductController(AppDbContext context)
        {
            _context = context;
        }

        public async Task<IActionResult> Index()
        {
            var products = await _context.Products.ToListAsync();
            return View(products);
        }

        public IActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create(Product product)
        {
            ModelState.Remove("Id");
            if (ModelState.IsValid)
            {
                _context.Products.Add(product);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(product);
        }

        public async Task<IActionResult> Edit(int id)
        {
            var product = await _context.Products.FindAsync(id);
            if (product == null)
            {
                return NotFound();
            }

            return View(product);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, Product product)
        {
            if (id != product.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Entry(product).State = EntityState.Modified;
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProductExists(product.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(product);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Delete(int id)
        {
            var product = await _context.Products.FindAsync(id);
            if (product == null)
            {
                return NotFound();
            }

            _context.Products.Remove(product);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool ProductExists(int id)
        {
            return _context.Products.Any(e => e.Id == id);
        }
    }
}

Step 9: Create Views

Create views for listing, creating, editing, and deleting products in the Views/Product directory.

Index.cshtml

@model List<Product>

<h1>Product List</h1>

<p>
    <a asp-action="Create" class="btn btn-primary">Create New</a>
</p>

<table class="table table-striped">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.First().Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.First().Price)
            </th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var product in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => product.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => product.Price)
                </td>
                <td>
                    <div class="btn-group" role="group">
                        <a asp-action="Edit" asp-route-id="@product.Id" class="btn btn-warning">Edit</a>
                        <form asp-action="Delete" asp-route-id="@product.Id" method="post" onsubmit="return confirm('Are you sure you want to delete this product?')">
                            @Html.AntiForgeryToken()
                            <button type="submit" class="btn btn-danger">Delete</button>
                        </form>
                    </div>
                </td>
            </tr>
        }
    </tbody>
</table>

Create.cshtml

@model Product

<h1>Create Product</h1>

<div>
    <form asp-action="Create" method="post">
        @await Html.PartialAsync("_ProductForm", Model)
        <button type="submit" class="btn btn-primary">Save Changes</button>
    </form>
</div>

Edit.cshtml

@model Product

<h1>Edit Product</h1>

<div>
    <form asp-action="Edit" method="post">
        @await Html.PartialAsync("_ProductForm", Model)
        <button type="submit" class="btn btn-primary">Save Changes</button>
    </form>
</div>

_ProductForm.cshtml (Partial View)

@model Product

<div class="form-group">
    <label asp-for="Name"></label>
    <input asp-for="Name" class="form-control" />
    <span asp-validation-for="Name" class="text-danger"></span>
</div>

<div class="form-group">
    <label asp-for="Price"></label>
    <input asp-for="Price" class="form-control" />
    <span asp-validation-for="Price" class="text-danger"></span>
</div>

<a asp-action="Index" class="btn btn-secondary">Back to List</a>

Step 9: Layout

Our layout appears to be like this for proper styling :

_Layout.cshtml

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - CodeFirstCRUD</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
    <link rel="stylesheet" href="~/CodeFirstCRUD.styles.css" asp-append-version="true" />
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8sh+Wy9HgQCF+6pB1Z+kmokEl4kgp5EqTqFZI" crossorigin="anonymous">
</head>
<body>
    <header>
        <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
            <div class="container-fluid">
                <a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">CodeFirstCRUD</a>
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                        aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
                    <ul class="navbar-nav flex-grow-1">
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
    </header>
    <div class="container">
        <main role="main" class="pb-3">
            @RenderBody()
        </main>
    </div>

    <footer class="border-top footer text-muted">
        <div class="container">
            &copy; 2024 - CodeFirstCRUD - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
        </div>
    </footer>
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="~/js/site.js" asp-append-version="true"></script>
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8sh+Wy9HgQCF+6pB1Z+kmokEl4kgp5EqTqFZI" crossorigin="anonymous"></script>
    @await RenderSectionAsync("Scripts", required: false)
</body>
</html>

P.S

Please let me know if there is anything that seems “broken” in the article.

You can also download the source code from: https://github.com/mcansener/CodeFirstCRUD

Stackademic

Thank you for reading until the end. Before you go:

Crud
Code First
Entity Framework Core
Net Core
Web Development
Recommended from ReadMedium