标签:
我们先从Code First模式出发,我们先创建三个类,来代表学生和课程的关系——Student,Course,Enrollment。有人可能会问,Enrollment是干嘛的?这是我们为了解耦所做的工作,如果我们单纯的让学生拥有多个课程,而一个课程有多个学生报名,那么就会形成多对多的关系,如果我们在其中添加一个中间层,那么则会形成多个一对一关系,从而完成解耦。
public class Student { public int ID { get; set; } public string LastName { get; set; } public string FirstMidName { get; set; } public DateTime EnrollmentDate { get; set; } public virtual ICollection<Enrollment> Enrollments { get; set; } } 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; } } 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; } }
之后,我们创建一个继承自DbContext的类,这个类主要承担起和数据库的交互:
public class SchoolContext:DbContext { public SchoolContext() : base("SchoolContext") { } 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>(); } }
OnModelCreating事件是在创建数据库中表时触发的,在方法中,我们只做了一件事,那就是让表的名字从复数变成单数。
虽然,我们启动时会在数据库中创建三个表,但是我们还没有数据,而且我们也没说连接哪个数据库。好吧,我们先写下连接字符串,前面已经说过了,这里贴一下就好了,EF自动调用它的:
<connectionStrings> <add name="SchoolContext" connectionString="data source=BLACKERXHUNTER\SQLEXPRESS;Initial Catalog=ContosoUniversity;Integrated Security=True;" providerName="System.Data.SqlClient" /> </connectionStrings>
然后,我们再创建数据:
public class SchoolInitializer : System.Data.Entity.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{CourseID=1050,Title="Chemistry",Credits=3,}, new Course{CourseID=4022,Title="Microeconomics",Credits=3,}, new Course{CourseID=4041,Title="Macroeconomics",Credits=3,}, new Course{CourseID=1045,Title="Calculus",Credits=4,}, new Course{CourseID=3141,Title="Trigonometry",Credits=4,}, new Course{CourseID=2021,Title="Composition",Credits=3,}, new Course{CourseID=2042,Title="Literature",Credits=4,} }; courses.ForEach(s => context.Courses.Add(s)); context.SaveChanges(); var enrollments = new List<Enrollment> { new Enrollment{StudentID=1,CourseID=1050,Grade=Grade.A}, new Enrollment{StudentID=1,CourseID=4022,Grade=Grade.C}, new Enrollment{StudentID=1,CourseID=4041,Grade=Grade.B}, new Enrollment{StudentID=2,CourseID=1045,Grade=Grade.B}, new Enrollment{StudentID=2,CourseID=3141,Grade=Grade.F}, new Enrollment{StudentID=2,CourseID=2021,Grade=Grade.F}, new Enrollment{StudentID=3,CourseID=1050}, new Enrollment{StudentID=4,CourseID=1050,}, new Enrollment{StudentID=4,CourseID=4022,Grade=Grade.F}, new Enrollment{StudentID=5,CourseID=4041,Grade=Grade.C}, new Enrollment{StudentID=6,CourseID=1045}, new Enrollment{StudentID=7,CourseID=3141,Grade=Grade.A}, }; enrollments.ForEach(s => context.Enrollments.Add(s)); context.SaveChanges(); } }
好吧,这个类稍稍有点长,不过其实结构挺简单的,我们创建了一个继承自DropCreateDatabaseIfModelChanges的泛型类,请把这个类的名字都出来——如果已经存在了一个同名的表在就重建一个表。嗯,就是这个意思。之后我们重载了seed方法,然后创建数据。对于每一个数据,我们调用了SchoolContext类里面的Set集合的Add方法,并保存,你可能会问我们为什么要些三遍SaveChanges,那是因为如果有哪个报错,就知道是哪里错了,它的使用也是超级简单的,只要在需要调用数据的地方添加这行代码即可:
private SchoolContext db = new SchoolContext();
标签:
原文地址:http://www.cnblogs.com/blackerXHunter/p/4555312.html