Loading...

crud operation in mvc 5 using entity framework

254 4________

In this video we will discuss, selecting, inserting, updating and deleting data in mvc using entity framework. Please watch crud operation in mvc 5 using entity framework And Watch my Channels MVC 5
We Will make database Social Networking and
We will be using tables Crud for this demo. You can get the sql script to create and populate these table from Part 1 of this video series.

Step 1: Create a new asp.net mvc 5 web application.

Step 2: Right click on the "Models" folder and add "ADO.NET Entity Data Model". Set Name = Model1.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 "Table" and select "Crud" table. Set "Model Namespace=Models" and click "Finish"

At this point we should have Crud entities generated.
a) Change Crud to CRUD
b) Change Crud nvigation property to CRUD
Right click anywhere on "Model1.edmx" file and select "Properties". In the properties window set
Entity Container Name = Social Networking Entities

Build the solution.
After this Step Rebuild the Application
Step 3: Right click on the "Controllers" folder and select Add - Controller MVC 5 Controller with views,Using Entity Framework. Set
Name =By Defualt name selected CrudsController
Template = MVC controller with read/write actions and views, using Entity Framework
Model class = Crud(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. CrudsController.cs file in "Controllers" folder
2. Index, Create, Edit, Detail and Delete views in "Crud" 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 = "Cruds"

Run the application again. Notice that, all the employees are listed on the index view. We can also create a new employee, edit an empoyee, 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.

Facebook
www.facebook.com/groups/2215354042080534/

コメント