The Contoso University Web Application
The application you'll be building in these tutorials is a simple university website.
Users can view and update student, course, and instructor information. A few of the screens you'll create are shown below.
本文中我们介绍codefirst开发模式
首先创建一个MVC应用:
创建完成以后,系统会自动创建一个模板页:
href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
@Html.Partial("_LogOnPartial")
id="menu">
@Html.ActionLink("Home", "Index", "Home")
@Html.ActionLink("About", "About", "Home")
@Html.ActionLink("Students", "Index", "Student")
@Html.ActionLink("Courses", "Index", "Course")
@Html.ActionLink("Instructors", "Index", "Instructor")
@Html.ActionLink("Departments", "Index", "Department")
@RenderBody()
创建数据MODEL:
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; }
}
}
using System;
using System.Collections.Generic;
namespace ContosoUniversity.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 Course Course { get; set; }
public virtual Student Student { get; set; }
}
}
using System;
using System.Collections.Generic;
namespace ContosoUniversity.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; }
}
}
他们的关系是,一个学生可以有多个登记:
public virtual ICollection<Enrollment> Enrollments { get; set; }
一门课程也有多个登记:
public virtual ICollection<Enrollment> Enrollments { get; set; }
而一个登记对应一门课程和一个学生。
建立完这三个实体类以后,就要创建数据库的CONTEXT:
在entityframe work中我们使用的是System.Data.Entity.DbContext class,在你的代码中要包含这个数据实体。代码如下:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using ContosoUniversity.Models;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace ContosoUniversity.Models
{
public class SchoolContext : DbContext
{
public DbSet<Student> Students { get; set; }
public DbSet<Enrollment> Enrollments { get; set; }
public DbSet<Course> Courses { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
}
代码中为每个实体都创建了一个DbSet的属性,在entity framework中,一个entityset 关联了一个数据库表,一个entity关系了一张表的一行数据。
OnModelCreating方法,表示当模型创建以后我们要做的动作,
在代码中表示组织数据库自动创建的表名为复数。如果你不这么做,那么产生的数据库表名为:
Students,Course,Enrollments.
然后在你的工程文件的web.config中配置数据库连接字段:
你不一定要创建连接字符串,如果你不创建,entityframework会自动在sqlserver express数据库中帮你创建。
注意我们配置的web.config 是工程目录下的。而不是在views目录下的。
在连接字符创中的name值,系统默认会去寻找和object context class名字相同的字符串。
初始化数据:
entity frame work能够自动创建数据库并初始化一些测试数据在你的应用中。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using ContosoUniversity.Models;
namespace ContosoUniversity.DAL
{
public class SchoolInitializer : DropCreateDatabaseIfModelChanges<SchoolContext>
{
protected override void Seed(SchoolContext context)
{
var students = new List<Student>
{
new Student { FirstMidName = "Carson", LastName = "Alexander", EnrollmentDate = DateTime.Parse("2005-09-01") },
new Student { FirstMidName = "Meredith", LastName = "Alonso", EnrollmentDate = DateTime.Parse("2002-09-01") },
new Student { FirstMidName = "Arturo", LastName = "Anand", EnrollmentDate = DateTime.Parse("2003-09-01") },
new Student { FirstMidName = "Gytis", LastName = "Barzdukas", EnrollmentDate = DateTime.Parse("2002-09-01") },
new Student { FirstMidName = "Yan", LastName = "Li", EnrollmentDate = DateTime.Parse("2002-09-01") },
new Student { FirstMidName = "Peggy", LastName = "Justice", EnrollmentDate = DateTime.Parse("2001-09-01") },
new Student { FirstMidName = "Laura", LastName = "Norman", EnrollmentDate = DateTime.Parse("2003-09-01") },
new Student { FirstMidName = "Nino", LastName = "Olivetto", EnrollmentDate = DateTime.Parse("2005-09-01") }
};
students.ForEach(s => context.Students.Add(s));
context.SaveChanges();
var courses = new List<Course>
{
new Course { Title = "Chemistry", Credits = 3, },
new Course { Title = "Microeconomics", Credits = 3, },
new Course { Title = "Macroeconomics", Credits = 3, },
new Course { Title = "Calculus", Credits = 4, },
new Course { Title = "Trigonometry", Credits = 4, },
new Course { Title = "Composition", Credits = 3, },
new Course { Title = "Literature", Credits = 4, }
};
courses.ForEach(s => context.Courses.Add(s));
context.SaveChanges();
var enrollments = new List<Enrollment>
{
new Enrollment { StudentID = 1, CourseID = 1, Grade = 1 },
new Enrollment { StudentID = 1, CourseID = 2, Grade = 3 },
new Enrollment { StudentID = 1, CourseID = 3, Grade = 1 },
new Enrollment { StudentID = 2, CourseID = 4, Grade = 2 },
new Enrollment { StudentID = 2, CourseID = 5, Grade = 4 },
new Enrollment { StudentID = 2, CourseID = 6, Grade = 4 },
new Enrollment { StudentID = 3, CourseID = 1 },
new Enrollment { StudentID = 4, CourseID = 1, },
new Enrollment { StudentID = 4, CourseID = 2, Grade = 4 },
new Enrollment { StudentID = 5, CourseID = 3, Grade = 3 },
new Enrollment { StudentID = 6, CourseID = 4 },
new Enrollment { StudentID = 7, CourseID = 5, Grade = 2 },
};
enrollments.ForEach(s => context.Enrollments.Add(s));
context.SaveChanges();
}
}
}
seed方法中,代码在这个方法中用OBJECT创建一个新的实体到数据库中,对于每一个实体类型,代码都创建一个集合,添加他们到DBSET属性中。然后保存转变到数据库中。
然后在global.asax.cs中添加如下代码:
在application_start方法中添加:
Database.SetInitializer<SchoolContext>(new SchoolInitializer());
笔记,当你实际开发部署应用的时候,要将这段代码去掉。切记。因为一旦系统欲行了该段代码,系统每次检测到模型层变化。都会重新创建数据库,然后初始化原始的测试数据。