In this article I will explain with an example, how to delete data from Database using Entity Framework in ASP.Net Core Razor Pages.
Database
I have made use of the following table Customers with the schema as follows. CustomerId is an Auto-Increment (Identity) column.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
Model
Following class is used as the Model class.
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.
using Microsoft.EntityFrameworkCore;
namespace EF_Delete_Razor_Core
{
public class DBCtx : DbContext
{
public DBCtx(DbContextOptions<DBCtx> options) : base(options)
{
}
public DbSet<Customer> Customers { get; set; }
}
}
Razor PageModel (Code-Behind)
The PageModel consists of following two Handler methods.
Handler method for handling GET operation
This Handler method handles the GET calls, for this particular example it is not required and hence left empty.
Handler method for handling Button Click and POST operation
This Handler method handles the POST call when the Submit Button is clicked and the Form is submitted.
This Handler method accepts an object of the Customer Model class as parameter. The values posted from the Form inside the Razor Page are received through this 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 ViewData object.
public class IndexModel : PageModel
{
private DBCtx Context { get; }
public IndexModel(DBCtx _context)
{
this.Context = _context;
}
public Customer Customer { get; set; }
public void OnGet()
{
}
public void OnPostSubmit(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();
ViewData["Message"] = "Customer record deleted.";
}
else
{
ViewData["Message"] = "Customer not found.";
}
}
}
Razor Page (HTML)
Inside the Razor Page, the ASP.Net TagHelpers is inherited.
The Razor Page consists of an HTML Form.
The HTML Form consists of a TextBox and a Submit Button.
The Submit Button has been set with the POST Handler method using the asp-page-handler attribute.
Note: In the Razor PageModel, the Handler method name is OnPostSubmit but here it will be specified as Submit when calling from the Razor HTML Page.
When the Delete button is clicked, the value of CustomerId is passed to the IndexModel.
@page
@model EF_Delete_Razor_Core.Pages.IndexModel
@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">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="width: 60px">
Id<br />
<input type="text" asp-for="Customer.CustomerId" style="width:50px" />
</td>
<td style="width: 200px">
<br />
<input type="submit" asp-page-handler="Submit" value="Delete" />
</td>
</tr>
</table>
</form>
@if (ViewData["Message"] != null)
{
<script type="text/javascript">
window.onload = function () {
alert("@ViewData["Message"]");
};
</script>
}
</body>
</html>
Screenshot
Downloads