I have an existing ASP.NET Core Angular Web application with the usual flairs. It comes with set of migration scripts (e.g. 20151227555555_AddIdentity.cs
, 20161227444444_AddCoreEntities.cs
etc) in a Migration/
code folder, each has Up
, Down
, and BuildTargetModel
methods.
then there is another folder called MigrationScript
that contains the equivalent of table creation SQL scripts (straight-up CREATE TABLE
...) for the various database entities.
I am used to running database management and recreation/restore via a database project that I can "publish
". I am new to the EF Migration way of doing it, so how do I run those migration C# scripts to create the database?
CodePudding user response:
The fact that the C# migration scripts already contains Up()
and Down()
methods means they are ready to be applied. (Note: usually the Up
and Down
methods are created by the Add-Migration migrationname
command).
Here is how to apply the C# entity framework migration scripts in an existing project:
Create the empty database with the correct database name. The database name is usually defined in the
aspsettings.json
file underConnectionStrings
group. Open Microsoft SQL Server Management Studio, create an empty database with the correct name.In Visual Studio Solution Explorer, right-mouse click on the project that contains the
Migration
folder (where the C# migration scripts are located), chooseManage NuGet Packages...
and look underInstalled
tab to see ifMicrosoft.EntityFrameWorkCore.Tools
package is installed, if not go ahead install it. Because that is the package that contains the EF migration commands, i.e.Add-Migration
,Update-Database
etcWith the
Microsoft.EntityFrameWorkCore.Tools
package installed in the project that contains theMigration
folder, open thePackage Manager Console
viaTools -> NuGet Package Manager -> Package Manager Console
menu options.At the
Package Manager Console
prompt, type the commandUpdate-Database
, then all of the migration scripts will be applied and the tables created in the local SQL Server database created in step 1.