8.2 数据库中放入初始数据

1.使用数据迁移中的seed方法

 

internal sealed class Configuration : DbMigrationsConfiguration<MvcApplication1.Models.StudentDB>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(MvcApplication1.Models.StudentDB context)
        {
            var students = new List<Student>
            {
                new Student{ Name="Jack",Sex="Male",Age=16},
                new Student{ Name="Joe",Sex="Male",Age=17},
                new Student{ Name="Jane",Sex="Female",Age=16},
                new Student{ Name="Rose",Sex="Female",Age=16},
                new Student{ Name="Anna",Sex="Female",Age=17}
            };
            students.ForEach(s => context.Students.AddOrUpdate(p => p.StudentId, s));
            context.SaveChanges();
        }
    }

 

2.在控制器动作中插入数据集合

3.直接打开数据库写入数据

posted @ 2017-05-11 14:51  RunningYY  阅读(220)  评论(0编辑  收藏  举报