Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 25 - Insert update delete in mvc using entity framework

Suggested Videos 
Part 22 - Including and excluding properties from model binding using interfaces
Part 23 - Why deleting database records using get request is bad
Part 24 - Deleting database records using post request in mvc

In this video we will discuss, selecting, inserting, updating and deleting data in mvc using entity framework. Please watch Part 24, before proceeding. 

We will be using tables tblDepartment and tblEmployee for this demo. You can get the sql script to create and populate these tables from Part 10 of this video series.

Step 1: Create a new mvc 4 web application.

Step 2: Right click on the "Models" folder and add "ADO.NET Entity Data Model". Set Name = EmployeeDataModel.edmx. 
Adding ADO.NET Entity Data Model

On the subsequent screen, select "Generate from database" option and click "Next".

On "Choose your data connection screen", click on "New Connection" button.

Specify the sql server name. In my case, I have sql server installed on my local machine. So I have set "Server Name=(local)". From "Select or enter a database name" dropdownlist, select the Database name and click "OK".
Connection to sql server database

Click "Next".

On "Choose your database objects" screen, expand "Tables" and select "tblDepartment" and "tblEmployee" tables. Set "Model Namespace=Models" and click "Finish"
Choose your database objects

At this point we should have tblDepartment and tblEmployee entities generated. 
a) Change tblDepartment to Department
b) Change tblEmployee to Employee
c) Change tblEmployees nvigation property to Employees
d) Change tblDepartment nvigation property to Department 
Employee and Department entities

Build the solution.

Step 3: Right click on the "Controllers" folder and select Add - Controller. Set 
Name = EmployeeController
Template = MVC controller with read/write actions and views, using Entity Framework
Model class = Employee(MVCDemo.Models)
Data Context Class = EmployeeContext(MVCDemo.Models)
Views = Razor

Finally click "Add".

At this point you should have the following files automatically added.
1. EmployeeController.cs file in "Controllers" folder
2. Index, Create, Edit, Detail and Delete views in "Employee" folder.

On Create and Edit views, please delete the following scripts section. We will discuss these in a later video session.
@section Scripts {

At this point, if you run the application by pressing CTRL + F5, you will get an error stating - The resource cannot be found. This is because, by default, the application goes to "HOME" controller and "Index" action.

To fix this
1. Open "RouteConfig.cs" file from "App_Start" folder
2. Set Controller = "Employee"

Run the application again. Notice that, all the employees are listed on the index view. We can also create a new employee, edit an employee, view their full details and delete an employee as well. However, there are few issues, with each of the views, which we will address in our upcoming videos.


  1. Sir, I am getting un supported context drop downlist for DataContextClass does not list EmployeeContext even if i build the solutiom more than once..please help..

    1. Watch the video, because you need to make EmployeeContext your connection string name, or else use the one called Sample, like on the blog here.

    2. For the unsupported context error, upgrade to "visual studio 2013"; upgrading resolved the issue for me.

    3. i have visual studio 2013 express version and still get this error.
      I named my connection string as Employeecontext as well . I have used connection string in web.config but still get the same error. However i included SapmleEntities from the dropdown list of select context and its working.

    4. Employee Controller is not generating scaffolding template and code and shows issues with alert. I have closed Visual Studio and opened again with Admin Privileges solved problem. This is for others reference.

  2. After watching video and this tutorial .
    When I constructed employeedatamodel, i can see the same 2 tables as you with the relation between them. In the EmployeeDataModel.edmx file, i change the name of the entity from tblEmployee to Employee and tblDepartment to Department
    and I Build Solution. And when i want to add a controller, i dont have any Employee model there to choose as a model class... what i have there is : tblEmployee (DML.Models), FilterConfig (DML), RouteConfig (DML),tblDepartment (DML.Models),tblEmployee (DML.Models)
    How to solve this problem?

    1. Hi i come to know the solution if you are using visual studio 2012 then you get this error .because it is some kind of manufacturing fault.....
      In case you are using visual studio 2010 then you will not get this error.

    2. The solution is in here:

    3. IF U USE THE VS2012 THEN
      SImply Right Click On The (MvcDemo) Project Name,
      Add New Item,
      SELECT Data and ADO.NET Entity Data Model
      Follow the same step as mentation in MVC Tutorials.

      I Think IT`s HELPFUT TO u.

    4., This worked for me, by moving entitydatamodel(employeedatamodel) to root directory, and the tbl prefix has gone. Thanks.

  3. Hi Does this auto generation work for mvc3 also?

    1. ya.

  4. Whats the impact of using entity framework in mvc4 application.

  5. Hi Venkat I got an error

    Server Error in '/MVCDemo25' Application.
    Login failed for user 'IIS APPPOOL\DefaultAppPool'.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Data.SqlClient.SqlException: Login failed for user 'IIS APPPOOL\DefaultAppPool'.

    Source Error:

    Line 20: {
    Line 21: var employees = db.Employees.Include("Department");
    Line 22: return View(employees.ToList());
    Line 23: }
    Line 24:

    Source File: c:\users\vigneswararao\documents\visual studio 2010\Projects\MVCDemo25\MVCDemo25\Controllers\EmployeeController.cs Line: 22

    Please help me

  6. Dear Venkat Sir,
    I have going through the MVC video of Part 25. I was having a problem, i cnt able to establish a new connection using entity, it showing some error like ,

    "Could not load file or assembly, the system cannot find the file specified."

    Can you help me with this, so i can able to move forward.
    Thanking you,

  7. Hi Venkat,

    Thanks for the Videos and the detailed blog on MVC. I tried to generate a Employee controller but it is creating templates for department, Please let me know where i am going wrong.

  8. Sir,
    In the tables i took there is no foreign key.Hence i dint get any relationship and also no navigation properties are can i take independent relationships?????

  9. Hi venkat sir,
    Thanks for the Videos and the detailed blog on MVC. I tried to generate a Employee controller but I am getting error "Unable to retrieve metadata for MVCDemo.Models.tblEmployee Format of the initialization string does not confirm to specification starting at index 173". please help me for this.......

    1. install Entity Framework 5
      go to Tools ->Library package manager ->Package mnager console

      Install-Package EntityFramework -Version 5.0.0-rc

  10. Not able to create controller

  11. Hello Venkat Sir, My question is when adding controller why we use Employee(MVCDemo.Models) why not Department(MVCDemo.Models) in Model Class

  12. Hello sir, I want to know that is edmx file make mvc application slow or not when I have large amount of data in database.

  13. Sir, if i Write data annotations in that auto generated class then if i update the model(edmx if any column changes ) yhen all the anotations will go away . what i need to do on that case.

  14. Hi guys,
    Model class = Employee(MVCDemo.Models)
    how this is coming... can anybody tell. I am setting entity container name as employeecontext. how this option is rising in drop down list.


  15. where you have created these two class files- employee.cs and department.cs??? you havnt mentioned. please give a way out


  16. Hello Sir,

    How do I update the entity framework key relation in the code. I followed all the mentioned steps to create Entity framework environment in code but later I found that in my database table I didn't have the foreign key relation between Employee and Department table so I have created a foreign key in Employee table referring Department table Department Id but the same foreign key relation is not updated in the entity framework code.

    Please suggest how to achieve this.


  17. Hi Sir,

    In my past interview, the Interviewer asked me about Dependency Injection in MVC. How will you implement?

    Can you please make an video on Dependency Injection in MVC?

  18. when i am going on step 3 model class is not found please solve this problem

  19. This comment has been removed by the author.

  20. Hi venkat sir,
    Thanks for the Videos and the detailed blog on MVC. I tried to generate a Employee controller but I am getting error "Unable to retrieve metadata for MVCDemo.Models.tblEmployee Format of the initialization string does not confirm to specification starting at index 173". please help me for this.......

  21. sir when i create the EmployeeController this error comes up"Could not find the CLR type For Models.Employee"

  22. Hello Sir,
    when i create the EmployeeController this error comes up"Could not retrieve metadata in Employee Model object refrence not set to an instance of object and

    1. Problem Resolved. But List of Employees is present. It is showing blank page

  23. How this can achieve in visual studio 15 with mvc 5, there is not option of such template i.e mvc 5 controller with read/write and views, using entity framework so kindly help me

    1. Hi jamal syed , In 2015 you have a option of MVC 5 controller with views using entity framework use it

  24. hello every body i really need a help when i created two table
    it appeared separated without navigition property and without relation please help me

  25. Hi Mostafa, this is happend because you have not mapped the Department table ID field-> with Employee table departmentId field.

    It means this primary key field ID of table Department is not properly related to the field departmentId of Employee table as foreign key.

    Hope I have given suitable reply.

  26. whenever Running new MVC application it showing
    The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

  27. After renaming the entities....i am trying to add the controller but..i am getting this error "unable to retriece metadata for mvcdemo.models.tblemployee". could not find the CLR type for model.emploee.....please help what i need to do...thank you..


It would be great if you can help share these free resources