[Reserved] EF Core migration analysis

Database migration is divided into two steps of NETCore

  1. Generated migration file
  2. Database migration
    specific commands that we do not discuss, we discuss only the most basic two after the command to do, and what problems we might encounter.
0. Introduction

The entire migration process involves three basic concepts, let's be clear about:

* 数据库:比如mysql下某个具体的数据库实例
* 源码:项目的源码,包括一个或多个DBContext  
* 迁移文件:缺省在项目源码根目录下的migrations目录下,包含迁移增量文件和镜像文件

Wherein the first step will need to generate the migration source file, according to a second step of updating the database file migration

1. Generate a migration file

dotnet ef migrations add mirgation1 -v

With the -v parameter can see the details of the implementation process, this process involves three basic concepts mentioned above, but also the whole process consists of three components:
1.1 execution dotnet build compile the entire source code, source code must be guaranteed in order to compile without error migration (nonsense).
1.2 implementation of the compiled dll not dotnet run, but dotnet exec. So the source code must be executed without problems, this is not nonsense, because we sometimes run the project are arguments, arguments can run, and run with no arguments can not be expressed without error.
In addition to this step will also find all inherited DbContext class, if there is more than one class, you will be prompted fails -context need to add parameters.

Note, mirgations add is not need to use the database, but the execution dll may need to connect to the database, but the database may not be ultimately true database, a test database can also be temporary.

dotnet exec --depsfile C:\Work\Github\jianshu_sample\MigrationsSample\MigrationsSample\bin\Debug\netcoreapp2.0\MigrationsSample.deps.json --additionalprobingpath C:\Users\liuyi\.nuget\packages --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig C:\Work\Github\jianshu_sample\MigrationsSample\MigrationsSample\bin\Debug\netcoreapp2.0\MigrationsSample.runtimeconfig.json "C:\Program Files\dotnet\sdk\2.1.400\DotnetTools\dotnet-ef\2.1.1\tools\netcoreapp2.1\any\tools\netcoreapp2.0\any\ef.dll" migrations add mirgation1 --assembly C:\Work\Github\jianshu_sample\MigrationsSample\MigrationsSample\bin\Debug\netcoreapp2.0\MigrationsSample.dll --startup-assembly C:\Work\Github\jianshu_sample\MigrationsSample\MigrationsSample\bin\Debug\netcoreapp2.0\MigrationsSample.dll --project-dir C:\Work\Github\jianshu_sample\MigrationsSample\MigrationsSample\ --language C# --working-dir C:\Work\Github\jianshu_sample\MigrationsSample\MigrationsSample --verbose --root-namespace MigrationsSample

1.3 the end of the execution of the last step, create or modify files and incremental image file.

Here often encounter a problem, steps above 1.2 when not end, resulting in the migration of stuck, because when there are 1.2 execution may start a cycle of death of the foreground thread, resulting in the process can not end normally.

img

image.png

All DBSet all fields and corresponding classes DBContext this search process, and then comparing the image file, if the image file does not exist, and generating a tables and all the fields are added, if present, in comparing the current source differences table structure and table structure of the image file and converting the difference into a delta file, and update the image file.
Therefore, when performing migrations add, in fact \ {Comparing is independent of image files and source code, and the database} color {red}, the image file can be appreciated that a complete database table structure implemented c # (above DataContextModelSnapshot.cs).
The delta file is a bunch of AddColum, DropColumn, CreateTable, RemoveTable other functions, represents the change is the addition of new fields, new fields and so on incremental delete operation.

2. Database Update

 dotnet ef database update -v

Update the database according to the above step 1 generated delta file. It can be divided into three steps:
2.1 compile the project, and 1.1 as
2.2 to run the project, and the same 1.2
2.3 _efmigrationshistory find records in a database table

img

image.png

To all current migration based on incremental file to find inside MigrationId, if there is a corresponding incremental file is ignored, not to perform again in all the incremental file list by AddColum, DropColumn, CreateTable, RemoveTable other functions to update the database. So this is actually a step, we need to step 2.1 to compile the source code of the project, in fact, we can skip the step 2.1

Original Address: https://www.jianshu.com/p/42bbbfa2993f

Guess you like

Origin www.cnblogs.com/ghhjanes/p/11280221.html