In this article I will explain with an example, how to update data (record) in Table using Entity Framework in C#.
 
 
Database
I have made use of the following table Customers with the schema as follows. CustomerId is an Auto-Increment (Identity) column.
Update Data (Record) in Table using Entity Framework in C#
 
I have already inserted few records in the table.
Update Data (Record) in Table using Entity Framework in C#
 
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 (Record) in Table using Entity Framework in C#
 
 
Controller
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();
    }
 
    [HttpPost]
    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;
                entities.SaveChanges();
                ViewBag.Message = "Customer record updated.";
            }
            else
            {
                ViewBag.Message = "Customer not found.";
            }
 
            return View();
        }           
    }
}
 
 
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>
 
<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: 150px">
                    Name<br/>
                    @Html.TextBoxFor(m => m.Name, new { style = "width:140px" })
                </td>
                <td style="width: 150px">
                    Country:<br/>
                   @Html.TextBoxFor(m => m.Country, new { style = "width:140px" })
                </td>
                <td style="width: 200px">
                    <br/>
                    <input type="submit" id="btnUpdate" value="Update" />
                </td>
            </tr>
        </table>
    }
    @if (ViewBag.Message != null)
    {
        <script type="text/javascript">
            window.onload = function () {
                alert("@ViewBag.Message");
            };
        </script>
    }
</body>
</html>
 
 
Screenshot
Update Data (Record) in Table using Entity Framework in C#
 
 
Downloads