时间:2021-07-01 10:21:17 帮助过:3人阅读
1.注释掉之前在Web.Config文件中添加的 contexts
节点。
<entityFramework> <!--<contexts> <context type="ContosoUniversity.DAL.SchoolContext,ContosoUniversity"> <databaseInitializer type="ContosoUniversity.DAL.SchoolInitailizer,ContosoUniversity" /> </context> </contexts>--> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="mssqllocaldb" /> </parameters> </defaultConnectionFactory>
2.同时修改连接字符串中的数据库名字为:ContosoUniversity2
<connectionStrings> <add name="SchoolContext" connectionString="Data Source=.;Initial Catalog=ContosoUniversity2;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/> <!--<add name="SchoolContext" connectionString="Data Source=(LocalDb)\mssqllocaldb;Initial Catalog=ContosoUniversity1;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>--> </connectionStrings>
这样第一次数据库迁移会创建一个新的数据库,不是必须这么做,但是这样比较好。
3.在vs中,工具->NuGet库程序包管理器->程序包管理器控制台
4.在控制台中键入以下命令:
enable-migrations
add-migration InitialCreate
enable-migrations命令在项目中创建了Migrations文件夹,并且在文件夹中有一个Configuration.cs文件可以用来配置迁移。
(如果在上述过程中,你省略了改变数据库名称这一步骤,数据库迁移会查找到已经存在的数据库,并自动添加add-migration命令,不过没关系,这样只不过会在你部署数据库之前不会运行测试的迁移代码。稍后运行update-database命令的时候,不会发生有任何变化,因为数据库已经存在了)
和你看到之前的初始化数据库类是一样的。Configuration类中包含一个Seed方法。这个方法是当你创建或者修改数据库之后可以插入或者修改数据。创建数据库以后会调用这个方法,每次数据模型改变导致数据库结构改变也会调用此方法。
删除重建数据库的时候使用初始化类的Seed方法添加测试数据,因为每次模型变动会导致数据库数据丢失。但使用了数据库迁移技术,原来的数据也会保留在数据库中,所以 在Seed方法中包含测试数据不是必须的。实际上真正项目部署的时候,我们并不希望Seed方法添加测试数据,而是添加我们需要的真实数据。
这节中,发布的时候我们将使用迁移,但是无论如何Seed方法都是添加测试数据,为了便于我们查看功能,使我们不需要手动添加大量数据。
1。修改Configuration.cs文件,下面代码将会在新的数据库中加载测试数据。
1 namespace ContosoUniversity.Migrations 2 { 3 using System; 4 using System.Data.Entity; 5 using System.Data.Entity.Migrations; 6 using System.Linq; 7 using ContosoUniversity.Models; 8 using System.Collections.Generic; 9 10 internal sealed class Configuration : DbMigrationsConfiguration<ContosoUniversity.DAL.SchoolContext> 11 { 12 public Configuration() 13 { 14 AutomaticMigrationsEnabled = false; 15 } 16 17 protected override void Seed(ContosoUniversity.DAL.SchoolContext context) 18 { 19 var students = new List<Student>{ 20 new Student { FirstMidName = "Carson", LastName = "Alexander", 21 EnrollmentDate = DateTime.Parse("2010-09-01") }, 22 new Student { FirstMidName = "Meredith", LastName = "Alonso", 23 EnrollmentDate = DateTime.Parse("2012-09-01") }, 24 new Student { FirstMidName = "Arturo", LastName = "Anand", 25 EnrollmentDate = DateTime.Parse("2013-09-01") }, 26 new Student { FirstMidName = "Gytis", LastName = "Barzdukas", 27 EnrollmentDate = DateTime.Parse("2012-09-01") }, 28 new Student { FirstMidName = "Yan", LastName = "Li", 29 EnrollmentDate = DateTime.Parse("2012-09-01") }, 30 new Student { FirstMidName = "Peggy", LastName = "Justice", 31 EnrollmentDate = DateTime.Parse("2011-09-01") }, 32 new Student { FirstMidName = "Laura", LastName = "Norman", 33 EnrollmentDate = DateTime.Parse("2013-09-01") }, 34 new Student { FirstMidName = "Nino", LastName = "Olivetto", 35 EnrollmentDate = DateTime.Parse("2005-08-11") } }; 36 students.ForEach(s => context.Students.AddOrUpdate(p => p.LastName, s)); 37 context.SaveChanges(); 38 39 var courses = new List<Course> { 40 new Course {CourseID = 1050, Title = "Chemistry", Credits = 3, }, 41 new Course {CourseID = 4022, Title = "Microeconomics", Credits = 3, }, 42 new Course {CourseID = 4041, Title = "Macroeconomics", Credits = 3, }, 43 new Course {CourseID = 1045, Title = "Calculus", Credits = 4, }, 44 new Course {CourseID = 3141, Title = "Trigonometry", Credits = 4, }, 45 new Course {CourseID = 2021, Title = "Composition", Credits = 3, }, 46 new Course {CourseID = 2042, Title = "Literature", Credits = 4, } 47 }; 48 courses.ForEach(c=>context.Courses.AddOrUpdate(p=>p.Title,c)); 49 context.SaveChanges(); 50 51 var enrollments = new List<Enrollment>{ 52 new Enrollment { 53 StudentID = students.Single(s => s.LastName == "Alexander").ID, 54 CourseID = courses.Single(c => c.Title == "Chemistry" ).CourseID, 55 Grade = Grade.A 56 }, 57 new Enrollment { 58 StudentID = students.Single(s => s.LastName == "Alexander").ID, 59 CourseID = courses.Single(c => c.Title == "Microeconomics" ).CourseID, 60 Grade = Grade.C 61 }, 62 new Enrollment { 63 StudentID = students.Single(s => s.LastName == "Alexander").ID, 64 CourseID = courses.Single(c => c.Title == "Macroeconomics" ).CourseID, 65 Grade = Grade.B 66 }, 67 new Enrollment { 68 StudentID = students.Single(s => s.LastName == "Alonso").ID, 69 CourseID = courses.Single(c => c.Title == "Calculus" ).CourseID, 70 Grade = Grade.B 71 }, 72 new Enrollment { 73 StudentID = students.Single(s => s.LastName == "Alonso").ID, 74 CourseID = courses.Single(c => c.Title == "Trigonometry" ).CourseID, 75 Grade = Grade.B 76 }, 77 new Enrollment { 78 StudentID = students.Single(s => s.LastName == "Alonso").ID, 79 CourseID = courses.Single(c => c.Title == "Composition" ).CourseID, 80 Grade = Grade.B 81 }, 82 new Enrollment { 83 StudentID = students.Single(s => s.LastName == "Anand").ID, 84 CourseID = courses.Single(c => c.Title == "Chemistry" ).CourseID 85 }, 86 new Enrollment { 87 StudentID = students.Single(s => s.LastName == "Anand").ID, 88 CourseID = courses.Single(c => c.Title == "Microeconomics").CourseID, 89 Grade = Grade.B 90 }, 91 new Enrollment { 92 StudentID = students.Single(s => s.LastName == "Barzdukas").ID, 93 CourseID = courses.Single(c => c.Title == "Chemistry").CourseID, 94 Grade = Grade.B 95 }, 96 new Enrollment { 97 StudentID = students.Single(s => s.LastName == "Li").ID, 98 CourseID = courses.Single(c => c.Title == "Composition").CourseID, 99 Grade = Grade.B 100 }, 101 new Enrollment { 102 StudentID = students.Single(s => s.LastName == "Justice").ID, 103 CourseID = courses.Single(c => c.Title == "Literature").CourseID, 104 Grade = Grade.B 105 } 106 }; 107 foreach(Enrollment e in enrollments) 108 { 109 var enrollmentInDatabase = context.Enrollments.Where(s=> 110 s.Student.ID==e.StudentID && 111 s.Course.CourseID==e.CourseID).SingleOrDefault(); 112 if (enrollmentInDatabase ==null) 113 { 114 context.Enrollments.Add(e); 115 } 116 } 117 context.SaveChanges(); 118 } 119 } 120 }
Seed方法将context作为传入参数,并使用context在数据库中添加新数据。对于每种实体类型,创建新的实体集合,并将这些集合添加到相应的Dbset中,然后保存对数据库做的更改。没必要像上面代码如此,在每组实体后面都添加SaveChanges方法。此处这么做是为了当向数据库中添加实体的时候如果抛出异常便于我们找到问题源头。
添加数据的时候使用了AddOrUpdate方法。每次数据迁移之后调用执行 update-database
命令都会运行Seed方法,但这样不仅仅是插入数据,因为创建数据库第一次数据迁移以后有些数据已经存在。更新插入的操作可以防止当你向数据库中添加已存在的数据时报错,但它会覆盖所有数据修改。有时我们不希望这种事情发生:比如我们测试的时候修改了数据,但是我们仍希望数据库修改以后,这些数据仍然保留。这时候我们用条件插入操作:只有在数据库中不存在该记录的时候才添加数据。
AddOrUpdate 方法的第一