In this article I will explain with an example, how to delete data from Database in ASP.Net Core (.Net Core 8) MVC.
 
 
 

Database

I have made use of the following table Customers with the schema as follow.
ASP.Net Core: Delete from Database
 
I have already inserted few records in the table.
ASP.Net Core: Delete from Database
 
Note: You can download the database table SQL by clicking the download link below.
            Download SQL file
 
 

Model

The Model class consists of the following properties.
public class Customer
{
    public int CustomerId { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
}
 
 

Database Context

Once the Entity Framework is configured and connected to the database table, the Database Context will look as shown below.
Note: For beginners in ASP.Net Core and Entity Framework, please refer my article ASP.Net Core: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework with ASP.Net Core.
 
using EF_Delete_MVC_Core.Models;
using Microsoft.EntityFrameworkCore;
 
namespace EF_Delete_MVC_Core
{
    public class DBCtx : DbContext
    {
        public DBCtx(DbContextOptions<DBCtx> options) : base(options)
        {
        }
 
        public DbSet<Customer> Customers { get; set; }
    }
}
 
 

Controller

The Controller consists of following Action methods.

Action method for handling GET operation

Inside this Action method, simply the View is returned.
 

Action method for handling POST operation

Inside this Action method, the Customer object is received as parameter.
The CustomerId value of the received Customer object is used to reference the Customer record using Entity Framework.
Once the record is referenced, the Customer record is deleted from the Customers table and a message is set in a ViewBag object.
public class HomeController : Controller
{
    private DBCtx Context { get; }
    public HomeController(DBCtx _context)
    {
        this.Context = _context;
    }
 
    public IActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public IActionResult Index(Customer customer)
    {
        Customer deletedCustomer = (from c in this.Context.Customers
                                    where c.CustomerId == customer.CustomerId
                                    select c).FirstOrDefault();
 
        if (deletedCustomer != null)
        {
            this.Context.Customers.Remove(deletedCustomer);
            this.Context.SaveChanges();
            ViewBag.Message = "Customer record deleted.";
        }
        else
        {
             ViewBag.Message = "Customer not found.";
        }
 
        return View();
    }
}
 
 

View

HTML Markup

Inside the View, in the very first line the Customer Model class is declared as Model for the View.
The View consists of an HTML Form which has been created using following Tag Helpers attributes.
asp-action – Name of the Action. In this case the name is Index.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
The HTML Form consists of a TextBox and a Submit Button.
When the Delete button is clicked, the value of CustomerId is passed to the Controller’s Action method.
Finally, the value of the ViewBag object is displayed using JavaScript Alert Message Box.
@model EF_Delete_MVC_Core.Models.Customer
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <form method="post" asp-action="Index" asp-controller="Home">
        <table border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td style="width: 60px">
                    Id<br />
                    <input type="text" asp-for="CustomerId" style="width:50px" />
               </td>
               <td style="width: 200px">
                   <br />
                   <input type="submit" id="btnDelete" value="Delete" />
               </td>
            </tr>
        </table>
 
    </form>
    @if (ViewBag.Message != null)
    {
        <script type="text/javascript">
            window.onload = function() {
                alert("@ViewBag.Message");
            };
       </script>
    }
</body>
</html>
 
 

Screenshot

ASP.Net Core: Delete from Database
 
 

Downloads