In this article I will explain with an example, how to get ID of last inserted record using SCOPE_IDENTITY when 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.
 
 
Database
I have made use of the following table Customers with the schema as follows. CustomerId is an Auto-Increment (Identity) column.
Using SCOPE_IDENTITY with Entity Framework in ASP.Net MVC
 
I have already inserted few records in the table.
Using SCOPE_IDENTITY with 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.
Using SCOPE_IDENTITY with Entity Framework in ASP.Net MVC
 
 
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 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();
    }
 
    [HttpPost]
    public ActionResult Index(Customer customer)
    {
        using (CustomerEntities entities = new CustomerEntities())
        {
            entities.Customers.Add(customer);
            entities.SaveChanges();
            int id = customer.CustomerId;
        }
        return View(customer);
    }
}
 
 
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.
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>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
    {
        <table cellpadding="0" cellspacing="0">
            <tr>
                <th colspan="2" align="center">Customer Details</th>
            </tr>
            <tr>
                <td>Name: </td>
                <td>
                    @Html.TextBoxFor(m => m.Name)
                </td>
            </tr>
            <tr>
                <td>Country: </td>
                <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>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Submit"/></td>
            </tr>
        </table>
    }
 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    @if (Model != null)
    {
        <script type="text/javascript">
            $(function () {
                alert("Inserted Customer ID: " + @Model.CustomerId);
            });
        </script>
    }
</body>
</html>
 
 
Screenshots
The Form
Using SCOPE_IDENTITY with Entity Framework in ASP.Net MVC
 
CustomerId displayed after data insert
Using SCOPE_IDENTITY with Entity Framework in ASP.Net MVC
 
 
Downloads