ASP.NET MVC CODE MIGRATION

0
ASP.NET MVC CODE MIGRATION :-

It is used to dynamically change the database schema and Table Schema in code first approach ,By default entity framework has no migration support under code first technique for security concern .

If we want to apply migration under project then first open package manager console and type

enable-migrations


It will create migrations folder under project open configuration.cs and write following code

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

        protected override void Seed(MvcApplication3.Models.StudentDB context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }
    }

now open dbcontext file  and write following code under constructor


public class StudentDB:DbContext
    {
        public StudentDB()
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<StudentDB,MvcApplication3.Migrations.Configuration>());
        }
        public DbSet<Student> Students { get; set; }
        public DbSet<Employee> Employeies { get; set; }
    }



Tags

Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)