Create classes for each entity using Entity Framework Code First

Leave a Comment
In the following sections you will create a class for each one of these entities.

The Student Entity

In the Models folder, create Student.cs and replace the existing code with the following code:
using System;
using System.Collections.Generic;
namespace MVC3EntityFrameworkDemo.Models
{
public class Student
{
public int StudentID {get;set;}
public string LastName {get;set;}
public string FirstMidName {get;set;}
public DateTime EnrollmentDate {get;set;}
public virtualICollection<Enrollment> Enrollments {get;set;}
}
}

The StudentID property will become the primary key column of the database table that corresponds to this class. By default, the Entity Framework interprets a property that's named ID or classnameID as the primary key.

The Enrollments property is a navigation property. Navigation properties hold other entities that are related to this entity. In this case, the Enrollments property of a Student entity will hold all of the Enrollment entities that are related to that Student entity. In other words, if a given Student row in the database has two related Enrollment rows (rows that contain that student's primary key value in their StudentID foreign key column), that Student entity's Enrollments navigation property will contain those two Enrollment entities.

Navigation properties are typically defined as virtual so that they can take advantage of an Entity Framework function called lazy loading. (Lazy loading will be explained later.) If a navigation property can hold multiple entities (as in many-to-many or one-to-many relationships), its type must be ICollection.

The Enrollment Entity

In the Models folder, create Enrollment.cs and replace the existing code with the following code:
using System;
using System.Collections.Generic;
namespace MVC3EntityFrameworkDemo.Models
{
public class Enrollment
{
public int EnrollmentID {get;set;}
public int CourseID {get;set;}
public int StudentID {get;set;}
public decimal?Grade {get;set;}
public virtual CourseCourse {get;set;}
public virtual StudentStudent{get;set;}
}
}

The question mark after the decimal type declaration indicates that the Grade property is nullable. A grade that's null is different from a zero grade - null means a grade hasn't been assigned yet, while zero means a zero grade has been assigned.

The StudentID property is a foreign key, and the corresponding navigation property is Student. An Enrollment entity is associated with one Student entity, so the property can only hold a single Student entity (unlike the Student.Enrollments navigation property you saw earlier, which can hold multiple Enrollment entities).

The CourseID property is a foreign key, and the corresponding navigation property is Course. An Enrollment entity is associated with one Course entity.

The Course Entity

In the Models folder, create Course.cs, replacing the existing code with the following code:
using System;
using System.Collections.Generic;
namespace MVC3EntityFrameworkDemo.Models
{
public class Course
{
public int CourseID{get;set;}
public string Title{get;set;}
public int Credits{get;set;}
public virtual ICollection<Enrollment> Enrollments {get;set;}
}
}

The Enrollments property is a navigation property. A Course entity can be related to any number of Enrollment entities.

0 comments:

Post a Comment