In this article I will explain with an example, how to delete record from Database using Entity Framework in ASP.Net MVC.
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.
Entity Framework Model
Once the Entity Framework is configured and connected to the database table, the Model will look as shown below.
Controller
The Controller consists of following two Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
Action method for handling POST operation for Deleting
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 in the Customer Entities.
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
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Customer customer)
{
using (CustomersEntities entities = new CustomersEntities())
{
Customer deletedCustomer = (from c in entities.Customers
where c.CustomerId == customer.CustomerId
select c).FirstOrDefault();
if (deletedCustomer != null)
{
entities.Customers.Remove(deletedCustomer);
entities.SaveChanges();
ViewBag.Message = "Customer record deleted.";
}
else
{
ViewBag.Message = "Customer not found.";
}
return View();
}
}
}
View
Inside the View, in the very first line the Entity Framework Customer Model class is declared as Model for the View.
The View consists of an HTML Form which has been created using the Html.BeginForm method with the following parameters.
ActionName – Name of the Action. In this case the name is Index.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
There is a TextBox created for capturing value for CustomerId using the Html.TextBoxFor method.
There’s also a Submit Button and when the Button is clicked, the Form is submitted.
After the Form is submitted, the ViewBag object returned from the Controller is checked for NULL and if it is not NULL then ViewBag object is displayed using JavaScript Alert MessageBox.
@model Delete_EF_MVC.Customer
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="width: 60px">
Id<br />
@Html.TextBoxFor(m => m.CustomerId, new { style = "width:50px" })
</td>
<td style="width: 200px">
<br />
<input type="submit" id="btnDelete" value="Delete" />
</td>
</tr>
</table>
}
@if (ViewBag.Message != null)
{
<script type="text/javascript">
window.onload = function () {
alert("@ViewBag.Message");
};
</script>
}
</body>
</html>
Screenshot
Downloads