EFCore database migration command sorting, efcore database migration
Preface
Because the development of new projects using. net core often involves data command migration, the two methods of database migration are respectively integrated with EFCore today.
1. Package Manager Console and Package Manager Console (PMC)
-If you use visual studio for development, we recommend using the PMC migration method. This method supports both efcore and ef migration.
2. Command line interface (CLI)
-This method is applicable to database migration during cross-platform development, that is, it can be separated from visual studio. For example, you can use vs code or directly open the cmd console to perform operations.
1. The following describes the comparison of the two methods, and then detailed descriptions of vs and vscode.
Migration command description |
CLI Commands |
PMC command |
Create migration: migrationname indicates the migration name. |
Dotnet ef migrations add migrationName |
Add-migration migrationName |
Remove migration (delete the latest migration) |
Dotnet ef migrations remove |
Remove-migration |
Migrate all applications (migrate files to the database) |
Dotnet ef database update |
Update-database |
Migration with the specified version |
Dotnet ef database update migrationName |
Update-database migrationName |
Generate scripts of the corresponding version |
Dotnet ef migrations script |
Script-Migration |
View migration list |
Dotnet ef migrations list |
|
View database context information |
Dotnet ef dbcontext info |
|
2. Package Manager Console, Package Manager Console (PMC)
Open the visual studio package management console. First, you must be good at using pmc help. Enter get-help to view help information. As prompted, We can enter get-help about_entityframework to obtain help information, after entering the command, we should be able to see a domineering Mustang avatar. The following is the help documentation for efcore.
The add-migration command is usually not problematic. Enter the command and press enter to enter the migration name.
Remove-migration. If you want to delete the last migration, execute it directly.
After the first step of "update-database" is successfully added to a migration file, you can use this command to directly update the file to the database. By default, all migration tasks are performed. If you want to specify the migration file name, simply add the migration file name, for example, update-database migrationName is also equivalent to version rollback. For example, if version 1, 2, and 3 are available, I want to roll back to version 1 and directly update-database 1, at this time, the database has been updated to version 1, and the migration files 2 and 3 can be deleted in remove-migration twice.
The Script-Migration command is used to generate the SQL statement corresponding to the Migration file, which is somewhat different from the previous ef command, if no parameter is added, this command generates the SQL statement corresponding to all migration files by default.
Script-Migration-From migrationName1-To migrationName2-Context ContextName
Interestingly, it does not generate the SQL statement of the migration file corresponding to from. That is to say, the above write will only generate the SQL statement of migrationName2, so the problem arises, what should I do if I need to generate the SQL statement for the first migration file? After viewing the official documentation, you must specify the from parameter as 0, that is, Script-Migration-From 0.
Finally, you can use Get-Help <cmdlet-name> to obtain the corresponding command documentation, such as get-help update-databa.
3. cross-platform Command line interface (CLI)
After entering dotnet ef on the vscode command terminal, you can also see the portrait of the domineering Wild Horse and list the help information.
1. dotnet ef migrations add generate a migration
2. dotnet ef migrations remove Delete the latest migration
3. dotnet ef database update generates and migrates data to the database. Similar to the above pmc command, you can perform version rollback by adding the specified migration as a parameter.
4. dotnet ef migrations script is similar to pmc. If there are no parameters, all SQL scripts are generated by default, but the parameter format is slightly different: dotnet ef migrations script migrationName1 migrationName2; the SQL script that directly follows the migration name like this is to generate migrationName1 to migrationName2.
Related links:
Official documents: https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/
Some changes to efcore relative to ef: https://msdn.microsoft.com/magazine/mt614250
Efcore reverse engineering dbfirst: https://code.msdn.microsoft.com/How-to-using-Entity-1464feea
Permanent update address: http://siyouku.cn/article/6871.html
Asp.net core development framework: https://github.com/2014fighting/CodeFrame.Web.git