Exercise 5-1: contoso University campus Management 1

Source: Internet
Author: User

** Purpose: to develop applications with complex models.

The contoso campus management system includes the management of students, courses, and teachers.

1. Create an MVC Web Application

The display effect is shown in the following figure.

Ii. Create a data model

1. Create a Student Entity

using System;using System.Collections.Generic;namespace ContosoUniversity.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 virtual ICollection<Enrollment> Enrollments { get; set; }    }}

Primary Key for studentid attribute. EF uses ID or classnameid as the primary key by default.

The enrollments attribute is a navigation attribute that owns the object associated with this object. Here, the Student Entity has all the relevant course registration entities. For example, in the database, there are three records in the course registry, and there are three enrollment rows in the navigation attribute. After the navigation attribute is created, the foreign key is automatically generated for the database data table. If the navigation property can contain multiple objects (for example, one-to-many, many-to-many), The type must be a set, such as icollection.

Navigation attributes are usually defined as virtual, so that you can take advantage of the lazy loading function of EF. Delayed loading means that the data is not required for the moment, instead of loading immediately, but can be postponed until it is used. Delayed loading is a very important data access feature that can effectively reduce interaction with data sources (note that the interaction mentioned here is not the number of interactions, but the amount of data that interacts ), to improve program performance.

2. Create a course entity

using System.Collections.Generic;using System.ComponentModel.DataAnnotations.Schema;namespace ContosoUniversity.Models{    public class Course    {        [DatabaseGenerated(DatabaseGeneratedOption.None)]        public int CourseID { get; set; }        public string Title { get; set; }        public int Credits { get; set; }                public virtual ICollection<Enrollment> Enrollments { get; set; }    }}

The enrollments attribute is the navigation attribute.

Courseid considering the possibility of special data rules, using [databasegenerated (databasegeneratedoption. None)] will not use the sequence number automatically generated by the database.

3. Create a registration entity

namespace ContosoUniversity.Models{    public enum Grade    {        A, B, C, D, F    }    public class Enrollment    {        public int EnrollmentID { get; set; }        public int CourseID { get; set; }        public int StudentID { get; set; }        public Grade? Grade { get; set; }                public virtual Course Course { get; set; }        public virtual Student Student { get; set; }    }}

The grade attribute is an Enum Enumeration type ,? This attribute can be empty.

The studentid attribute is a foreign key and the corresponding navigation attribute is student. A registered entity is associated with a student entity. Therefore, the navigation attribute has a Student Entity instead of the previous icollection set.

Courseid is the same.

4. Create a database Context

Create a Dal (data access layer) folder and create a schoolcontext. CS class in the folder.

using ContosoUniversity.Models;using System.Data.Entity;using System.Data.Entity.ModelConfiguration.Conventions;namespace ContosoUniversity.DAL{    public class SchoolContext : DbContext    {        public DbSet<Student> Students { get; set; }        public DbSet<Enrollment> Enrollments { get; set; }        public DbSet<Course> Courses { get; set; }    }}

This Code creates a dbset attribute for each object set. In EF technology, an entity set corresponds to a database table, and an entity corresponds to a row in the table.

Modify the Web. config file.

<add name="SchoolContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=ContosoUniversity;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\ContosoUniversity.mdf" providerName="System.Data.SqlClient" />

By default, EF searches for the connection string name of the dbcontext class name. In this connection string, you have added a localdb database named contosouniversity. MDF in the app_data folder. If no connection string is specified, EF creates a new one for you.
5. Enable code first migrations

(1) Open the Package Manager Console

(2) enter the command enable-migrations-contexttypename schoolcontext

The configuration class contains a seed method that allows you to update model data when creating a database.

 

(3)

Exercise 5-1: contoso University campus Management 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.