English 中文(简体)
MVC Framework - Models
  • 时间:2024-11-05

MVC Framework - Models


Previous Page Next Page  

The component ‘Model’ is responsible for managing the data of the apppcation. It responds to the request from the view and it also responds to instructions from the controller to update itself.

Model classes can either be created manually or generated from database entities. We are going to see a lot of examples for manually creating Models in the coming chapters. Thus in this chapter, we will try the other option, i.e. generating from the database so that you have hands-on experience on both the methods.

Create Database Entities

Connect to SQL Server and create a new database.

Connect SQL Server

Now run the following queries to create new tables.

CREATE TABLE [dbo].[Student]( 
   [StudentID]      INT           IDENTITY (1,1) NOT NULL, 
   [LastName]       NVARCHAR (50) NULL, 
   [FirstName]      NVARCHAR (50) NULL, 
   [EnrollmentDate] DATETIME      NULL, 
   PRIMARY KEY CLUSTERED ([StudentID] ASC) 
)  

CREATE TABLE [dbo].[Course]( 
   [CourseID] INT           IDENTITY (1,1) NOT NULL, 
   [Title]    NVARCHAR (50) NULL, 
   [Credits]  INT           NULL, 
   PRIMARY KEY CLUSTERED ([CourseID] ASC) 
)  

CREATE TABLE [dbo].[Enrollment]( 
   [EnrollmentID] INT IDENTITY (1,1) NOT NULL, 
   [Grade]        DECIMAL(3,2) NULL, 
   [CourseID]     INT NOT NULL, 
   [StudentID]    INT NOT NULL, 
   PRIMARY KEY CLUSTERED ([EnrollmentID] ASC), 
      CONSTRAINT [FK_dbo.Enrollment_dbo.Course_CourseID] FOREIGN KEY ([CourseID]) 
   REFERENCES [dbo].[Course]([CourseID]) ON DELETE CASCADE, 
      CONSTRAINT [FK_dbo.Enrollment_dbo.Student_StudentID] FOREIGN KEY ([StudentID]) 
   REFERENCES [dbo].[Student]([StudentID]) ON DELETE CASCADE 
)

Generate Models Using Database Entities

After creating the database and setting up the tables, you can go ahead and create a new MVC Empty Apppcation. Right-cpck on the Models folder in your project and select Add → New Item. Then, select ADO.NET Entity Data Model.

Add New Model Step 1

Add New Model Step 2

In the next wizard, choose Generate From Database and cpck Next. Set the Connection to your SQL database.

Add New Model Test Connection

Select your database and cpck Test Connection. A screen similar to the following will follow. Cpck Next.

Add New Model Test Connection Step 2

Select Tables, Views, and Stored Procedures and Functions. Cpck Finish. You will see the Model View created as shown in the following screenshot.

New MVC Model

The above operations would automatically create a Model file for all the database entities. For example, the Student table that we created will result in a Model file Student.cs with the following code −

namespace MvcModelExample.Models { 
   using System; 
   using System.Collections.Generic; 
     
   pubpc partial class Student { 
      
      pubpc Student() { 
         this.Enrollments = new HashSet(); 
      } 
     
      pubpc int StudentID { get; set; } 
      pubpc string LastName { get; set; } 
      pubpc string FirstName { get; set; } 
      pubpc Nullable EnrollmentDate { get; set; } 
      pubpc virtual ICollection Enrollments { get; set; } 
   } 
}
Advertisements