MVC Framework Models - MVC Framework

What is the use of Models in MVC framework?

‘Model’ component manages application data. It responds to the request from the view and also responds to instructions from the controller to update itself.

Model classes can either be created manually or generated from database entities. In this chapter, let’s try the other option of generating from the database.

Create Database Entities

Connect to SQL Server and create a new database.

sql server

Now run below queries for creating new tables.

Generate Models Using Database Entities

After creating the database and setting up the tables, create a new MVC Empty Application. For creating, right-click on the Models folder in your project and select Add → New Item. Then, select ADO.NET Entity Data Model.



In the next wizard, select Generate from Database and click on Next. Set the Connection to your SQL database.


Select your database and click on Test Connection. Below screen will be displayed and in that Click on Next.


Select Tables, Views, and Stored Procedures and Functions and Click Finish. A Model View will be created as shown in below screenshot.

mvc model

Above operations will automatically create a Model file for all the database entities. For example, Student table which is created will result in a Model file Student.cs with the below code

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

MVC Framework Topics