In this article I will explain with an example, how to update Data into Database using Entity Framework Database First Approach in ASP.Net MVC.
I have made use of the following table Customers with the schema as follows. CustomerId is an Auto-Increment (Identity) column.
Update Data in Database using Entity Framework Database First Approach
I have already inserted few records in the table.
Update Data in Database using Entity Framework Database First Approach
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
Entity Framework Model
Once the Entity Framework is configured and connected to the database table, the Model will look as shown below.
Note: For beginners in ASP.Net MVC and Entity Framework, please refer my article ASP.Net MVC: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework.
Update Data in Database using Entity Framework Database First Approach
The Controller consists of two Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
Action method for Updating
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 values of Name and Country are updated and the changes are updated into the Customers table.
Based on whether the reference of the Customer is found or not, an appropriate message is set in the ViewBag object.
public class HomeController : Controller
    // GET: Home
    public ActionResult Index()
        return View();
    public ActionResult Index(Customer customer)
        using (CustomersEntities entities = new CustomersEntities())
            Customer updatedCustomer = (from c in entities.Customers
                                        where c.CustomerId == customer.CustomerId
                                        select c).FirstOrDefault();
            if (updatedCustomer != null)
                updatedCustomer.Name = customer.Name;
                updatedCustomer.Country = customer.Country;
                ViewBag.Message = "Customer record updated.";
                ViewBag.Message = "Customer not found.";
            return View();
Inside the View, in the very first line the Customer Entity 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.
Also the View consists of three TextBoxes and a Button.
When the Update button is clicked, the values of CustomerId, Name and Country are passed to the Action method inside the Controller using Form submission.
Finally, the value of the ViewBag object is displayed using JavaScript Alert Message Box.
@model Update_EF_MVC.Customer
    Layout = null;
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width" />
    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
        <table border="0" cellpadding="0" cellspacing="0">
                <td style="width: 60px">
                    @Html.TextBoxFor(m => m.CustomerId, new { style = "width:50px" })
                <td style="width: 150px">
                    @Html.TextBoxFor(m => m.Name, new { style = "width:140px" })
                <td style="width: 150px">
                   @Html.TextBoxFor(m => m.Country, new { style = "width:140px" })
                <td style="width: 200px">
                    <input type="submit" id="btnUpdate" value="Update" />
    @if (ViewBag.Message != null)
        <script type="text/javascript">
            window.onload = function () {
Update Data in Database using Entity Framework Database First Approach