Powershell script for entity framework core 2.1 code first, Visual Studio 2017, Package Manager Console

Published by Marian Galie-Andriescu on

Since recently I am a fan of entity framework core 2.1 code first, and I do not like to type a lot in the package manager console, I developed a small powershell script to speed up my work during development.

So first of all I advice you get yourself this plug in, it will give you nice colors and functionality when writing powershell scripts in Visual Studio 2017:
https://marketplace.visualstudio.com/items?itemName=AdamRDriscoll.PowerShellToolsforVisualStudio2017-18561

Next, I advice you to keep all your scripts and code together with the solution they are meant to work on. This means you will have this powershell script below in a file called “EFCommands.ps1” under your Infrastructure part of the solution, for example.

########################################################################################################
# Marian Galie-Andriescu        marian.galie.andriescu@gmail.com                                       #
# Shortcut functions for Entity Framework Core 2.1 example                                             #
########################################################################################################
function ef (
	$context=($paramMissing=$true),
	$operation=($paramMissing=$true),
	$migrationName) {
<# 
.Synopsis 
 Performs Entity Framework Core operations 
.DESCRIPTION Performs Entity Framework Core operations 
 USAGE: ef d|i (myDbcontext|myIdentitydbcontext) 
        a|r|u|s|d (Add migration|Remove migration|Update database|Script migration|Drop database) 
        MigrationName 
.EXAMPLE 
 Add a new migration to myDbcontext: 
 ef d a AddNewFeatureX 
 Update the database to migration AddNewFeatureX: 
 ef d u AddNewFeatureX 
 Update the database to last migration: 
 ef d u 
#>;
    If($local:paramMissing)
    {
      throw "USAGE: ef d|i (myDbcontext|myIdentitydbcontext) a|r|u|s|d (Add migration|Remove migration|Update database|Script migration|Drop database) MigrationName"
    }

    $contextName =
    switch ($context)
    {
        "d" {"myDbContext"}
        "i" {"myIdentityDbContext"}
    }

    $directoyName =
    switch ($context)
    {
        "d" {"Db\Migrations"}
        "i" {"Identity\Migrations"}
    }

    switch ($operation)
    {
	"a" {
	       Write-Host -ForegroundColor Cyan "Add-migration $migrationName -C $contextName -O $directoyName -P Infrastructure"
	       Add-migration $migrationName -C $contextName -O $directoyName -P Infrastructure
	    }
	"r" {  Write-Host -ForegroundColor Cyan "Remove-migration -C $contextName -P Infrastructure"
	       Remove-migration -C $contextName -P Infrastructure
	    }
	"u" {
	       Write-Host -ForegroundColor Cyan "Update-Database -M $migrationName -C $contextName -P Infrastructure"
	       Update-Database -M $migrationName -C $contextName -P Infrastructure
	    }
    "s" {
           Write-Host -ForegroundColor Cyan "Script-Migration -C $contextName -P Infrastructure"
	       Script-Migration -C $contextName -P Infrastructure
        } 
    "d" {
	       Write-Host -ForegroundColor Cyan "Drop-Database -C $contextName -P Infrastructure"
	       Drop-Database -C $contextName -P Infrastructure
        }
	default 
	    { 
	       Write-Host -ForegroundColor Red "Unknown Operation: $operation" 
	    }
    }
}

You will need to change this script to suit your needs.

Next, have a look in the Package Manager Console where your profile file is located, execute command $profile:

Just keep in mind, your Default project should be the one that is your start-up project, this project should have as well your database connection strings for your db contexts.

Next, if the profile file does not exist, create the profile file called NuGet_profile.ps1 in the path as shown above. You can execute for example command:
mkdir C:\Users\Marian\Documents\WindowsPowerShell\
to create the all the needed directories, and then you can create profile file with notepad. The job of this file is to automatically load our function as defined in EFCommands.ps1 file, and will contain a line as the one below, of course you will need to change the path with your own  location:

. d:\work\MyProject\SRC\Infrastructure\scripts\EFCommands.ps1

If you change the script in EFCommands.ps1 you can execute the command above as well in the Package Manager Console to update the function. Now you are done, and it is time to enjoy your work: with 7 key strokes you have generated the sql scripts for your MyDbContext:

 

Categories: PowerShell