EF architecture ~ Migrations and efmigrations in the CodeFirst production environment

Source: Internet
Author: User
Tags class manager

EF architecture ~ Migrations and efmigrations in the CodeFirst production environment

Back to directory

Migrations refers to migration. It is the product of the code first mode of EF. It means that the code changes are reflected in the database, which has two environments, the first is the local development environment, and the other is the production environment of the server. The local development environment mainly uses the update-database of the package management tool to complete database migration (change ), in the production environment, it seems a little troublesome, because you will not put the program source code and VS development tools in the production environment, haha.

Please refer to my article on local database migration

EF architecture ~ CodeFirst data migration and database deletion prevention

Data Migration in the production environment on the server

If we have the following migration files:

public partial class manager : DbMigration    {        public override void Up()        {            DropForeignKey("dbo.WebDataSettings", "WebDataCtrlId", "dbo.WebDataCtrls");            DropForeignKey("dbo.WebDataSettings", "DepartmentId", "dbo.WebDepartments");            RenameColumn(table: "dbo.WebDataSettings", name: "DepartmentId", newName: "WebDepartmentsId");            RenameIndex(table: "dbo.WebDataSettings", name: "IX_DepartmentId", newName: "IX_WebDepartmentsId");            AddForeignKey("dbo.WebDataSettings", "WebDataCtrlId", "dbo.WebDataCtrls", "ID", cascadeDelete: true);            AddForeignKey("dbo.WebDataSettings", "WebDepartmentsId", "dbo.WebDepartments", "ID", cascadeDelete: true);        }                public override void Down()        {            DropForeignKey("dbo.WebDataSettings", "WebDepartmentsId", "dbo.WebDepartments");            DropForeignKey("dbo.WebDataSettings", "WebDataCtrlId", "dbo.WebDataCtrls");            RenameIndex(table: "dbo.WebDataSettings", name: "IX_WebDepartmentsId", newName: "IX_DepartmentId");            RenameColumn(table: "dbo.WebDataSettings", name: "WebDepartmentsId", newName: "DepartmentId");            AddForeignKey("dbo.WebDataSettings", "DepartmentId", "dbo.WebDepartments", "ID");            AddForeignKey("dbo.WebDataSettings", "WebDataCtrlId", "dbo.WebDataCtrls", "ID");        }    }

Run the following command in Local VS to migrate the production plan, that is, the SQL statement.

Update-Database-Script-SourceMigration: $ InitialDatabase-TargetMigration: manage

It generates the corresponding SQL script, and we can run it on the server.

Idempotence

The SQL script generated by Migration is idempotent, that is, when you execute the SQL script multiple times, the results are the same and do not play a negative role.

Back to directory

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.