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 asp.net mvc 4 web application.
Step 2: Right click on the "Models" folder and add "ADO.NET Entity Data Model". Set Name = EmployeeDataModel.edmx.
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".
Click "Next".
On "Choose your database objects" screen, expand "Tables" and select "tblDepartment" and "tblEmployee" tables. Set "Model Namespace=Models" and click "Finish"
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
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 {
@Scripts.Render("~/bundles/jqueryval")
}
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.
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 asp.net mvc 4 web application.
Step 2: Right click on the "Models" folder and add "ADO.NET Entity Data Model". Set Name = EmployeeDataModel.edmx.
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".
Click "Next".
On "Choose your database objects" screen, expand "Tables" and select "tblDepartment" and "tblEmployee" tables. Set "Model Namespace=Models" and click "Finish"
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
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 {
@Scripts.Render("~/bundles/jqueryval")
}
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.
Sir, I am getting un supported context error..my drop downlist for DataContextClass does not list EmployeeContext even if i build the solutiom more than once..please help..
ReplyDeleteWatch the video, because you need to make EmployeeContext your connection string name, or else use the one called Sample, like on the blog here.
DeleteFor the unsupported context error, upgrade to "visual studio 2013"; upgrading resolved the issue for me.
Deletei have visual studio 2013 express version and still get this error.
DeleteI 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.
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.
DeleteAfter watching video and this tutorial .
ReplyDeleteWhen 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?
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.....
DeleteIn case you are using visual studio 2010 then you will not get this error.
The solution is in here:
Deletehttp://stackoverflow.com/questions/12256519/entity-framework-issue-in-vs2012-rename-properties-not-sticking
IF U USE THE VS2012 THEN
DeleteSImply Right Click On The (MvcDemo) Project Name,
Add New Item,
SELECT Data and ADO.NET Entity Data Model
AND
Follow the same step as mentation in MVC Tutorials.
I Think IT`s HELPFUT TO u.
http://stackoverflow.com/questions/12256519/entity-framework-issue-in-vs2012-rename-properties-not-sticking, This worked for me, by moving entitydatamodel(employeedatamodel) to root directory, and the tbl prefix has gone. Thanks.
DeleteHi Does this auto generation work for mvc3 also?
ReplyDeleteya.
DeleteIS ALSO WORK IN MVC3 USING AUTO GENERATION
Whats the impact of using entity framework in mvc4 application.
ReplyDeleteHi Venkat I got an error
ReplyDeleteServer 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
Dear Venkat Sir,
ReplyDeleteI 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,
Pranay
Hi Venkat,
ReplyDeleteThanks 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.
Sir,
ReplyDeleteIn the tables i took there is no foreign key.Hence i dint get any relationship and also no navigation properties are available....so can i take independent relationships?????
Hi venkat sir,
ReplyDeleteThanks 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.......
install Entity Framework 5
Deletego to Tools ->Library package manager ->Package mnager console
NOW OPEN A PACKAGE MANAGER CONSOLE WINDOW
PM>
Install-Package EntityFramework -Version 5.0.0-rc
Not able to create controller
ReplyDeleteHello Venkat Sir, My question is when adding controller why we use Employee(MVCDemo.Models) why not Department(MVCDemo.Models) in Model Class
ReplyDeleteHello sir, I want to know that is edmx file make mvc application slow or not when I have large amount of data in database.
ReplyDeleteSir, 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.
ReplyDeleteHi guys,
ReplyDeleteModel 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.
thanks
neel
where you have created these two class files- employee.cs and department.cs??? you havnt mentioned. please give a way out
ReplyDeletethanks
neel
Hello Sir,
ReplyDeleteHow 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.
Thanks
Abhishek
Hi Sir,
ReplyDeleteIn 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?
when i am going on step 3 model class is not found please solve this problem
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi venkat sir,
ReplyDeleteThanks 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.......
sir when i create the EmployeeController this error comes up"Could not find the CLR type For Models.Employee"
ReplyDeleteHello Sir,
ReplyDeletewhen 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
Problem Resolved. But List of Employees is present. It is showing blank page
DeleteHow 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
ReplyDeleteHi jamal syed , In 2015 you have a option of MVC 5 controller with views using entity framework use it
Deletehello every body i really need a help when i created two table
ReplyDeleteit appeared separated without navigition property and without relation please help me
Hi Mostafa, this is happend because you have not mapped the Department table ID field-> with Employee table departmentId field.
ReplyDeleteIt 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.
whenever Running new MVC application it showing
ReplyDeleteThe resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
ex:
http://localhost:80/MvcApplication/Employee/Edit
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..
ReplyDelete