迁移文件中的up和down方法之间的区别

时间:2016-04-15 14:44:22

标签: asp.net-mvc database-migration

我正在学习asp.net mvc 当我向我的项目添加迁移时,它会添加两个文件,其中包含三种方法(种子,向上和向下)。

我不明白上下方法有什么区别 你能帮我解释一下吗?

这些是我的上下方法: 这是什么意思!!

 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");
        }

提前致谢

1 个答案:

答案 0 :(得分:3)

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.