Preface
Recently plan to use. NET core to write a simple back-Office system to practice practiced hand
Then we used the entity Framework Core
Found in the garden some of the articles are not so detailed, for novice small white, may be a bit ignorant.
A few details were deliberately arranged.
Body
Data Migration
First, EF Core has a different point from the previous EF6,
Microsoft's official website lists the different points: https://docs.microsoft.com/zh-cn/ef/efcore-and-ef6/features
Install the EF core NuGet package
To use the EF core, install the NuGet package for the database provider that you want to use. For example, if you target SQL Server, you will install Microsoft.EntityFrameworkCore.SqlServer
.
If you plan to use migration, you should also install the Microsoft.EntityFrameworkCore.Tools
package.
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Tools
Note: The following explanation is part of the previous article has appeared, in order to take care of students who did not read the previous article, the code is simply posted out. No more explaining.
First create a new Model class Sysuser:
Then set up the Efcorecontext class
Automatically create a database
At this point, our database and data are already available.
Data Migration
This is the point, when we have a database table, we need to modify the field, how to migrate
First open the Vs-> tool->nuget Package Manager-Packages Management Console
Input command:add-migration init(execute this command project to generate a directory (migration))
We will find that the model Assembly has more than one folder for migrations inside there are 3 files. The following:
Xxxxxxx_init.cs the Master migration file. Contains ()
the actions required for application migration (in) and recovery ( ()
in).
Xxxxxxx_init. Designer.cs-Migrate the metadata file. Contains information used by EF.
EFCoreContextModelSnapshot.cs-A snapshot of the current model. Used primarily to determine what changes have occurred when the next migration was added.
Then we execute the command:
Update-database
If your database is not created for the first time, it will execute successfully.
If you have previously created a database. But the first time you create a migration: It will fail.
Sure enough, we have the table structure already exists,
Let's look at the Xxxxxx_init.cs file:
As you can see, the first migration method generated here is based on the new migration: Instead of modifying it.
We delete the code from the Up () down ()
This is the equivalent of this migration, did not do any operation.
Then we'll create a real migration version:
First add an email field and change the length of the username to 60
And then we started migrating.
add-migration X2 (This is the migration version name, customizable and can be rolled back depending on the version)
After the migration file is successfully created, we update the database.
Update-database x2
Then the email field is added, the username length has been modified, and the username data is still there.
Remove Migration
When we just created a migration that didn't apply to the database, we found that we needed to change the entity. Then we can delete the migrated version without the app.
The following commands are executed:
Remove-migration
Note that this is a migration that has not been applied and can be removed. If you have applied it, you will receive an error message
Migration Rollback
There are times when we need to roll back to a previous migration version. For example, when we deploy, the development version and the stable version must be different.
Then we'll use the rollback command.
Execute as follows :
Update-database here fill in the name of the version that needs to be rolled back
We execute update-database init
Then we will find that the length of the X2 is back to 30, and the email field is gone ....
Generate Migration SQL
Sometimes, our production database needs to create a library with a script. So we can also generate SQL scripts directly from entities. The command is as follows:
script-Migration
Extrapolate:script-Migration x2 This script can customize the version number that needs to be migrated. File name, the context in which the migration script needs to be generated
Database migration of the ASP. NET Core series "Six" Entity Framework Core