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