Tags

,


MIGRATE.EXE

Code First Migrations Command Line Utility, Applies any pending migrations to the database.

migrate <assembly>

[configurationType]

[/targetMigration]

[/startUpDirectory]

[/startUpConfigurationFile]

[/startUpDataDirectory]

[/connectionStringName]

[/connectionString]

[/connectionProviderName]

[/force]

[/verbose]

assembly : Specifies the name of the assembly that  contains the migrations configuration type.

[configurationType]:  Specifies the name of the migrations configuration type. If omitted, Code First Migrations will attempt to locate a single    migrations configuration type in the specified assembly.

[/connectionProviderName] : Specifies the provider invariant name of the connection string.

[/connectionString]: Specifies the the connection string to use. If omitted, the context’s default connection will  be used.

[/connectionStringName]: Specifies the name of the connection string to  use from the specified configuration file. If omitted, the context’s default connection will  be used.

[/force]: Indicates that automatic migrations which might incur data loss should be allowed.

[/startUpConfigurationFile] :  Specifies the Web.config or App.config file of  your application.

[/startUpDataDirectory]:    Specifies the directory to use when resolving connection strings containing the |DataDirectory| substitution string.

[/startUpDirectory] :   Specifies the working directory of your application.

[/targetMigration] :      Specifies the name of a particular migration to update the database to. If omitted, the current  model will be used.

[/verbose]: Indicates that the executing SQL and additional diagnostic information should be output to the  console window.

When you install Entity Framework using NuGet migrate.exe will be inside the tools folder of the downloaded package. In <project  older>\packages\EntityFramework.<version>\tools

Once you have migrate.exe then you need to copy it to the location of the assembly that contains your migrations.

If your application targets .NET 4, and not 4.5, then you will need to copy the Redirect.config into the location as well and rename it migrate.exe.config. This is so that migrate.exe gets the correct binding redirects to be able to locate the Entity Framework assembly.

Migrate to the latest migration

Migrate.exe MyMvcApplication.dll /startupConfigurationFile=”..\web.config”

When running migrate.exe the only mandatory parameter is the assembly, which is the assembly that contains the migrations that you are trying to run, but it will use all convention based settings if you do not specify the configuration file.

Migrate to a specific migration

Migrate.exe MyApp.exe /startupConfigurationFile=”MyApp.exe.config” /targetMigration=”AddTitle”

If you want to run migrations up to a specific migration, then you can specify the name of the migration. This will run all previous migrations as required until getting to the migration specified.

Specify working directory

Migrate.exe MyApp.exe /startupConfigurationFile=”MyApp.exe.config” /startupDirectory=”c:\MyApp”

If you assembly has dependencies or reads files relative to the working directory then you will need to set startupDirectory.

Specify migration configuration to use

Migrate.exe MyAssembly CustomConfig /startupConfigurationFile=”..\web.config”

If you have multiple migration configuration classes, classes inheriting from DbMigrationConfiguration, then you need to specify which is to be used for this execution. This is specified by providing the optional second parameter without a switch as above.

Provide connection string

Migrate.exe BlogDemo.dll /connectionString=”Data Source=localhost;Initial Catalog=BlogDemo;Integrated Security=SSPI” /connectionProviderName=”System.Data.SqlClient”

If you wish to specify a connection string at the command line then you must also provide the provider name. Not specifying the provider name will cause an exception.

Ex:

D:\Demo\eSample\eSample\bin>Migrate.exe eSample.dll /connecti

onstring=”Server=noiclt248;Database=test;UID=XXX;PWD=XXX;Pooling=true;Max P

ool Size=200;Trusted_Connection=False;Persist Security Info=True” /connectionPro

viderName=”System.Data.SqlClient”

Ex:

Migrate.exe ABC.XYX.Data.dll /connectionstring=”Server=XXX;Database=test;UID=xx;PWD=xxx;Pooling=true;Max Pool Size=200;Trusted_Connection=False;Persist Security Info=True” /connectionProviderName=”System.Data.SqlClient”

 

 

Enjoy!!

Advertisements