In this article I will explain with an example, how to insert data (record) in database using Entity Framework in ASP.Net MVC Razor.
When the Form is submitted, the value of the submitted Form fields will be fetched using Entity Framework Model class object and will be inserted into database using Entity Framework. After successful insert, the ID of the last inserted record fetched using SCOPE_IDENTITY will be displayed using JavaScript Alert Message Box.
Entity Framework by default internally uses SCOPE_IDENTITY for fetching ID of the last inserted record.
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.
I have made use of the following table Customers with the schema as follows. CustomerId is an Auto-Increment (Identity) column.
Insert data using Entity Framework in ASP.Net MVC
I have already inserted few records in the table.
Insert data using Entity Framework in ASP.Net MVC
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.
Insert data using Entity Framework in ASP.Net MVC
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 handling POST operation
The Action method for POST operation accepts an object of the Entity Framework Customer Model class as parameter. The values posted from the Form inside the View are received through this parameter.
The received values are then inserted into the SQL Server database table using Entity Framework.
The CustomerId of the inserted record is available in the Entity Framework Customer Model object after the SaveChanges method is executed.
The Entity Framework Customer Model object is returned back to the View.
public class HomeController : Controller
    // GET: Home
    public ActionResult Index()
        return View();
    public ActionResult Index(Customer customer)
        using (CustomerEntities entities = new CustomerEntities())
            int id = customer.CustomerId;
        return View(customer);
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.
ActionNameName 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 one TextBox field created for capturing value for Name using the Html.TextBoxFor method. While for capturing the Country value, a DropDownList with Country options is created using the Html.DropDownListFor function.
There’s also a Submit Button at the end of the Form and when the Button is clicked, the Form is submitted.
After the Form is submitted, the Entity Framework Customer Model object returned from the Controller is checked for NULL and if it is not NULL then the newly inserted CustomerId is displayed using JavaScript Alert MessageBox.
@model EntityFramework_Insert_MVC.Customer
    Layout = null;
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width"/>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
        <table cellpadding="0" cellspacing="0">
                <th colspan="2" align="center">Customer Details</th>
                <td>Name: </td>
                    @Html.TextBoxFor(m => m.Name)
                <td>Gender: </td>
                    @Html.DropDownListFor(m => m.Country, new List<SelectListItem>
                   { new SelectListItem{Text="India", Value="India"},
                     new SelectListItem{Text="China", Value="China"},
                     new SelectListItem{Text="Australia", Value="Australia"},
                     new SelectListItem{Text="France", Value="France"},
                     new SelectListItem{Text="Unites States", Value="Unites States"},
                     new SelectListItem{Text="Russia", Value="Russia"},
                     new SelectListItem{Text="Canada", Value="Canada"}},
                     "Please select")
                <td><input type="submit" value="Submit"/></td>
    <script type="text/javascript" src=""></script>
    @if (Model != null)
        <script type="text/javascript">
            $(function () {
                alert("Inserted Customer ID: " + @Model.CustomerId);
The Form
Insert data using Entity Framework in ASP.Net MVC
CustomerId displayed after data insert
Insert data using Entity Framework in ASP.Net MVC