Tuesday, April 22, 2014

Entity Framework Migrations

Below are some commands which are useful when it comes to migrating your database schema when using Entity Framework.

These commands needs to be run through Tools > NuGet Package Manager > Package Manager Console , under Visual Studio.

The code for this can be found under this folder ...\packages\EntityFramework.6.1.0\tools\EntityFramework.psm1

Enable-Migrations: Enables Code First Migrations in a project.
Add-Migration: Scaffolds a migration script for any pending model changes.
Update-Database: Applies any pending migrations to the database.
Get-Migrations: Displays the migrations that have been applied to the target database.

Enable-Migrations
Enables Code First Migrations in a project.

Syntax
Enable-Migrations [-EnableAutomaticMigrations] [[-ProjectName] <String>]
  [-Force] [<CommonParameters>]
Description
Enables Migrations by scaffolding a migrations configuration class in the project. If the  target database was created by an initializer, an initial migration will be created (unless automatic migrations are enabled via the EnableAutomaticMigrations parameter).

Parameters
-EnableAutomaticMigrations
Specifies whether automatic migrations will be enabled in the scaffolded migrations configuration. If ommitted, automatic migrations will be disabled.

-ProjectName <String>
Specifies the project that the scaffolded migrations configuration class will be added to. If omitted, the default project selected in package manager console is used.

-Force
Specifies that the migrations configuration be overwritten when running more than once for given project.

<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer and OutVariable. For more information, type: get-help about_commonparameters.


Add-Migration
Scaffolds a migration script for any pending model changes.

Syntax
Add-Migration [-Name] <String> [-Force]
  [-ProjectName <String>] [-StartUpProjectName <String>]
  [-ConfigurationTypeName <String>] [-ConnectionStringName <String>]
  [-IgnoreChanges] [<CommonParameters>]

Add-Migration [-Name] <String> [-Force]
  [-ProjectName <String>] [-StartUpProjectName <String>]
  [-ConfigurationTypeName <String>] -ConnectionString <String>
  -ConnectionProviderName <String> [-IgnoreChanges] [<Common Parameters>]
Description
Scaffolds a new migration script and adds it to the project.

Parameters
-Name <String>
Specifies the name of the custom script.

-Force
Specifies that the migration user code be overwritten when re-scaffolding an existing migration.

-ProjectName <String>
Specifies the project that contains the migration configuration type to be used. If ommitted, the default project selected in package manager console is used.

-StartUpProjectName <String>
Specifies the configuration file to use for named connection strings. If omitted, the specified project’s configuration file is used.

-ConfigurationTypeName <String>
Specifies the migrations configuration to use. If omitted, migrations will attempt to locate a single migrations configuration type in the target project.

-ConnectionStringName <String>
Specifies the name of a connection string to use from the application’s configuration file.

-ConnectionString <String>
Specifies the the connection string to use. If omitted, the context’s default connection will be used.

-ConnectionProviderName <String>
Specifies the provider invariant name of the connection string.

-IgnoreChanges
Scaffolds an empty migration ignoring any pending changes detected in the current model. This can be used to create an initial, empty migration to enable Migrations for an existing database. N.B. Doing this assumes that the target database schema is compatible with the current model.

<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer and OutVariable. For more information, type: get-help about_commonparameters.


Update-Database
Applies any pending migrations to the database.

Syntax
Update-Database [-SourceMigration <String>]
  [-TargetMigration <String>] [-Script] [-Force] [-ProjectName <String>]
  [-StartUpProjectName <String>] [-ConfigurationTypeName <String>]
  [-ConnectionStringName <String>] [<CommonParameters>]

Update-Database [-SourceMigration <String>] [-TargetMigration <String>]
  [-Script] [-Force] [-ProjectName <String>] [-StartUpProjectName <String>]
  [-ConfigurationTypeName <String>] -ConnectionString <String>
  -ConnectionProviderName <String> [<CommonParameters>]
Description
Updates the database to the current model by applying pending migrations.

Parameters
-SourceMigration <String>
Only valid with -Script. Specifies the name of a particular migration to use as the update’s starting point. If ommitted, the last applied migration in the database will be used.

-TargetMigration <String>
Specifies the name of a particular migration to update the database to. If ommitted, the current model will be used.

-Script
Generate a SQL script rather than executing the pending changes directly.

-Force
Specifies that data loss is acceptable during automatic migration of the
database.

-ProjectName <String>
Specifies the project that contains the migration configuration type to be used. If ommitted, the default project selected in package manager console is used.

-StartUpProjectName <String>
Specifies the configuration file to use for named connection strings. If omitted, the specified project’s configuration file is used.

-ConfigurationTypeName <String>
Specifies the migrations configuration to use. If omitted, migrations will attempt to locate a single migrations configuration type in the target project.

-ConnectionStringName <String>
Specifies the name of a connection string to use from the application’s configuration file.

-ConnectionString <String>
Specifies the the connection string to use. If omitted, the context’s default connection will be used.

-ConnectionProviderName <String>
Specifies the provider invariant name of the connection string.

<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer and OutVariable. For more information, type: get-help about_commonparameters.


Get-Migrations
Displays the migrations that have been applied to the target database.

Syntax
Get-Migrations [-ProjectName <String>] [-StartUpProjectName <String>]
  [-ConfigurationTypeName <String>] [-ConnectionStringName <String>]
  [<CommonParameters>]

Get-Migrations [-ProjectName <String>] [-StartUpProjectName <String>]
  [-ConfigurationTypeName <String>] -ConnectionString <String>
  -ConnectionProviderName <String> [<CommonParameters>]
Description
Displays the migrations that have been applied to the target database.

Parameters
-ProjectName <String>
Specifies the project that contains the migration configuration type to be used. If ommitted, the default project selected in package manager console is used.

-StartUpProjectName <String>
Specifies the configuration file to use for named connection strings. If omitted, the specified project’s configuration file is used.

-ConfigurationTypeName <String>
Specifies the migrations configuration to use. If omitted, migrations will attempt to locate a single migrations configuration type in the target project.

-ConnectionStringName <String>
Specifies the name of a connection string to use from the application’s configuration file.

-ConnectionString <String>
Specifies the the connection string to use. If omitted, the context’s default connection will be used.

-ConnectionProviderName <String>
Specifies the provider invariant name of the connection string.

<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer and OutVariable. For more information, type: get-help about_commonparameters.

No comments: