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.直接打开数据库写入数据

相关文章:

  • 2022-02-09
  • 2021-11-28
  • 2022-12-23
  • 2022-12-23
  • 2022-01-17
  • 2021-10-19
  • 2021-06-17
  • 2021-12-30
猜你喜欢
  • 2021-09-28
  • 2021-11-10
  • 2022-12-23
  • 2021-11-02
  • 2022-01-25
  • 2021-10-28
  • 2021-11-18
相关资源
相似解决方案