How to Create Data Model using Database in MVC Application

February 13, 2014 , , 0 Comments

As I have discussed in earlier article, MVC model folder have all the classes that may be used for application logic in the application. By default an MVC application have some of the models like AccountModels, LogOnModels, RegisterModels etc.

We will create a new model class by using some simple steps.

  • Right click on the Model folder and add a new class named Student.cs
  • Insert some properties in that class like written below.

namespace MvcApplication1.Models
    public class Student
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string City { get; set; }
public class StudentContext: DbContext
public DbSet<Student> Students { get; set; }

This class have all the four properties which are the columns in the student table of the database. Now to work with this class we have to add a controller (after debugging this application) class in the Controllers folder with these simple steps.

  • Right click on the Controller folder and add a new Controller. A window will appear with some of the options to be inputted.
  • Name the controller StudentController and more options like shown in the window.

How to Create Data Model using Database in MVC Application

After clicking on Add button visual web developer will add StudentController.cs file under the Controller folder, and Student folder under the Views folder.

The last steps of this article is to add Database views which are used for UI purpose in the application. There is a plus point in this MVC application i.e. we don’t need to add required views because they are automatically added by the visual web developer.

Lookout under the Views>Student Folder

  • Index.cshtml
  • Create.cshtml
  • Delete.cshtml
  • Details.cshtml
  • Edit.cshtml

Subhash Sharma

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google