Search code examples
asp.net-mvcdatabase-migration

the difference between the up and down methods in the migration file


i'm learning asp.net mvc when i add a migration to my project it add two files with three methods (seed, up and down).

i don't understand what is the difference between the up and down methods can you explain it for me ?

these are my up and down methods : what does this mean !!

 public override void Up()
        {
            AddColumn("dbo.Projets", "Description", c => c.String());
            AddColumn("dbo.Projets", "UtilisateurID", c => c.Int(nullable: false));
            AlterColumn("dbo.Projets", "etat", c => c.Int());
            CreateIndex("dbo.Projets", "UtilisateurID");
            AddForeignKey("dbo.Projets", "UtilisateurID", "dbo.Utilisateurs", "UtilisateurID", cascadeDelete: true);
        }

        public override void Down()
        {
            DropForeignKey("dbo.Projets", "UtilisateurID", "dbo.Utilisateurs");
            DropIndex("dbo.Projets", new[] { "UtilisateurID" });
            AlterColumn("dbo.Projets", "etat", c => c.String());
            DropColumn("dbo.Projets", "UtilisateurID");
            DropColumn("dbo.Projets", "Description");
        }

thanks in advance


Solution

  • DB Migrations make changes to your database to reflect the changes made to the Entity Framework Model. These changes are added to the database with the Up method.

    When you want to rollback a change (e.g. by rolling back a changeset in TFS or Git), the changes in the database have to be rolled back also because otherwise your Entity Framework model is out-of-sync with the database. This is what the Down method is for. It undo's all changes to the database that were done when the Up method was run against the database.

    The Seed method gives you the ability to Insert, Update or Delete data which is sometimes needed when you change the model of the database. So the Seed method is optional and only necessary when you need to modify existing data or add new data to make the model working.