It’s easy. How long will you need to create a full functional MVC Application? You will be able to create a fully functional ASP.NET MVC 3 application within less than 5 minutes. It will take a few mouse clicks and you will get an application that is possible of performing all CRUD(Create-Read-Update-Delete) operations.
Step 1: Create an Empty ASP.NET MVC 3 Application and Attach Database
As we did in previous examples, you can create an empty application using Visual Studio 2010.Then go to the main menu of Visual Studio 2010. Under the
Project tab, select
Add ASP.NET Folder and select
App_Data. App_Data is the folder that is going to keep the database of the application that we create. If you are going to work with a database that is created in SQL Server, then you do not need to create this folder. The database can be connected once we are creating the Model.
Step 2: Creating the Model with Entity Framework
Right click on the Models folder of the Solution Explorer and then proceed Add–>New Item. Then select Data from the installed templates that are available in the popup window. Choose ADO.NET Entity Data Model and change the name to MyModel. Then click Add. The Entity Data Model Wizard will open. Select Generate from Database and proceed Next.
Since my database is available in the App_Data folder, it automatically takes my Database and create the connection string. When you click Next, the and entity connection named
dbEntities will be created in the web.config file.
Then choose the Database objects. You can select the tables and views as your need. For this application, I select the available table in my database. Then click Finish.
A visual representation of the model will be created as a new file named MyModel.edmx.
Step 3: Creating the Controllers and the Views
First you will have to build the application for once. (Shift+F6) Then right click on the Controllers folder of the Solution explorer and proceed to Add–>Controller. Since I am planning to create the CRUD operations for the User table, I do give the controller name as UserController. Remember the convention, every controller must have the suffix “Controller” in its name. Then select the 2nd option of the Templates drop down list. If you did not build the application after creating the model, There will be an error saying no Model class is found. Then try building the application. In this case, I do select User class as the Model class and dbEntities class as the Data Context class. I keep the view engine as Razor and click the Add button.
Now we are done. This won’t take even 2-3 minutes when you are doing it next time.
Then you will see a sub-folder created named ‘User’ under the Views folder. This is because the controller name we gave was User. Under that folder, you will see 5 .cshtml files for CRUD operations and listing.
Run the application:
- To view user list: http://localhost:port/User
- To create a new User: http://localhost:port/User/Create
- To Edit an Existing User: http://localhost:port/User/Edit/Id
- To Delete an Existing User: http://localhost:port/User/Delete/Id
- To View an Existing User in detail: http://localhost:port/User/Details/Id
The source code of the application available here: Entity Framework Sample
Related Articles: