Friday, January 9, 2015

EF Migrations -Nuget Command Reference-1

 

Some Migration PS Commands :


  • Enable-Migrations     -EnableAutomaticMigrations 
     - Auto {Automatic migrations enabled for a project with only one DBContext}
     -ContextTypeName  AppDBContext
     -MigrationDirectory DirectoryName
  • Add-Migration
  • Update-Database       -configuration: ConfigurationFileName -Verbose
  • Get-Migrations

You are adding the table to an existing database, so you will first need to create a snapshot of the current database. By creating a snapshot of the current database, you can later create a migration that contains only the new table. To create a snapshot of the current database:
  1. Open the Package Manager Console
  2. Run the command enable-migrations
  3. Run the command add-migration initial –IgnoreChanges -force
  4. Run the command update-database
CTM: When we start with an existing database and we want to create our first migration, we first start with using
>add-migration InitialModel
Above command will create the migration file having up and down methods having all the tables that we have in our models.but we already have those tables in our database too.
So again we have to run this migration "InitialModel" but this time  we need to use a switch called -IgnoreChanges. With this switch we tell EF that whatever we have in our models now, we already have that in our existing database i.e
> add-migration  InitialModel    -IgnoreChanges -Force
With -Force switch, we are forcing this migration to be recreated. So, this time this "InitialModel" migration will change and both the up() and down()  methods will be empty.
One thing to note about code first migrations is that we can have only one pending migration at a time. So before we start making changes to our models, we need to run this (now) empty "InitialModel" migration on our database, so that the EF can now keep track of any further changes in the database. So we now use
> update-database
and now the _MigrationHistory table will get created in the database to keep track of changes.


Rollback To a Specific Migration:

Update-Database -TargetMigration: MigrationName
[Above code will revert back all the migrations up to the migration specified by MigrationName]
To Rollback all the way back to an Empty Database then use:
Update-Database    -TargetMigration: $InitialDatabase
Update-Database    -TargetMigration:()

To Generate Sql Script for the Migrations:
Update-Database   -Script  - SourceMigration: $InitialDatabase  
                                              - TargetMigration : MigrationName
$InitialDatabase = To generate script from an empty database.
[If we dont specify a -TargetMigration, then the latest migration will be used as Target.]
[If we dont specify a -SourceMigration, then migration will use the current state of the database.]
If, for deployment, we want to automatically upgrade the database by applying all the pending migrations during the application launch, then use
Database.SetInitializer(new MigrateDatabaseToLatestVersion<AppDBContext,Migrations.Configuration>() );
Migration.Configuration is the class that got added to our Migrations folder when we enabled the Migrations.


NUGET:

get-help  [package name]  ie. nuget, entityframework,bootstrap etc
{will fetch all available commands from the package  source}


PM> get-help  Update-Database   <-examples > <-detailed> <-full>

* To view list of packages available in the selected package source
    PM> Get-Package -ListAvailable

   Get-Package -ListAvailable  -Filter  {Name of the package}
   
   Get-Package -Filter {Keyword in package description}  -ListAvailable 

* To view all of the installed packages in the solution:
  PM> Get-Package

* To uninstall a package:
  PM> Uninstall-Package   <PackageName>   - RemoveDependencies

* To Check if any newer versions available for any installed package in the Application:
 PM> Get-Package -Updates

and to update the package:
PM> Update-Package <packageName>


*Nuget Powershell Profile {usually found at}
c:\Users\Document\WindowpowershellnugetProfile.psl

if this file doesnt exists, then to create it:

PM> mkdir -force (split-path $profile)
PM> notepad $profile





0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More