EntityFramework ile bir Db Migration(Taşımak)

Bir veri projede ilerlerken veri tabanın bozulmadan upgarde edilmesi veya yedeklenmesi gerebilir. Bunun için System.Data.Entity.Migrations  isimalanında gerekli komutlar mevcuttur.

Biz bir bir projede migrations etkin hale getirmek için, Package console Manegerdan,

enable-migrations -contexttypename School,

ekleme noktası yaratmak ve update etmek için

add-migration InitialCreate

update-database

Burada School contextimizin ismi, InitialCreate ise hangi işlemlerin yapıldığı belirden bir kayıt dosyası gibidir ve bir class dır.  Bu dosyalardan birini açtığımızda bunun gibi gözükebilir. Biz buraya database eklemek istedeğimiz verileri yazabilir.

internal sealed class Configuration : DbMigrationsConfiguration<ContosoUniversity.DAL.SchoolContext>
   {
      public Configuration()
      {
         AutomaticMigrationsEnabled = false;
      }

      protected override void Seed(School context)
      {
         var students = new List<Student>
            {
                new Student { FirstMidName = "Carson",   LastName = "Alexander", 
                    EnrollmentDate = DateTime.Parse("2010-09-01") },
                new Student { FirstMidName = "Peggy",    LastName = "Justice",   
                    EnrollmentDate = DateTime.Parse("2011-09-01") },
                new Student { FirstMidName = "Laura",    LastName = "Norman",    
                    EnrollmentDate = DateTime.Parse("2013-09-01") },
                new Student { FirstMidName = "Nino",     LastName = "Olivetto",  
                    EnrollmentDate = DateTime.Parse("2005-08-11") }
            };
         students.ForEach(s => context.Students.AddOrUpdate(p => p.LastName, s));
         context.SaveChanges();

         var courses = new List<Course>
            {
                new Course {CourseID = 1050, Title = "Chemistry",      Credits = 3, },
                new Course {CourseID = 2021, Title = "Composition",    Credits = 3, },
                new Course {CourseID = 2042, Title = "Literature",     Credits = 4, }
            };
         courses.ForEach(s => context.Courses.AddOrUpdate(p => p.Title, s));
         context.SaveChanges();

      }
   }

 

Ve bu class dosyısını çalışma anında yüklemek istersek aşağıdaki satırı yazmak gerekir.

var configuration = new Configuration();
            var migrator = new DbMigrator(configuration);
            migrator.Update();

Bir sonraki yazımızda görüşmek üzere hoşçakalın..

Add comment

The file '/Custom/Widgets/Calendar/widget.cshtml' does not exist.The file '/Custom/Widgets/Category list/widget.cshtml' does not exist.The file '/Custom/Widgets/Tag cloud/widget.cshtml' does not exist.The file '/Custom/Widgets/Page List/widget.cshtml' does not exist.The file '/Custom/Widgets/Month List/widget.cshtml' does not exist.