In the development environment, every time we make changes to the database, such as adding table fields. After we have changed the entity class, we only need to run the Update-database script in the NuGet package Management console:
Update-database
This migration feature provided by the Entity framework is very convenient and basically no longer has to open SQL Server Management Studio. So how do we synchronize our database changes in the release environment (server)?
One way is to run the Update-database script with the-verbose or-script parameters:
Update-database-script
This will generate SQL scripts each time, copy the generated SQL scripts, and then go to the server to execute.
But this is very inconvenient, in case I was in the middle of a local execution and forgot to run the corresponding SQL script on the service? The trouble that this brings is unimaginable.
We have a better way of doing update-database on the server as well as locally.
The following are the specific practices:
Copy the/packages/entityframework6.1.3/tools/migrate.exe file to the bin directory of the server publishing environment.
Save the following cmd command as a Migrate.bat file:
Migrate.exe example.ef.dll/startupconfigurationfile= ". \web.config "Pause
Exmaple.EF.dll is the DLL that you write the project for Entity Framework migration.
Migrate.exe can also not be placed in the bin directory, you can use to create a separate folder (such as tools) to store, but need to put EntityFramework.dll and EntityFramework.SqlServer.dll into this folder. In addition, the command line also specifies the parameter/startupdirectory: "Bin full directory", and the/startupconfigurationfile parameter also provides the full Web. config path.
This makes it convenient for each publisher to run this bat file, and ensures that the server's database version is always synchronized with the local.
Update the database using the Entity Framework's migration in the server