简体   繁体   中英

Managing Package Manager Console commands for Entity Framework in project

Entity Framework commands entered in the Package Manager Console, like Add-Migration and Update-Database can get fairly long and complex when when dealing with solutions that have a "non-standard" build.

With syntax and options like:

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

Add-Migration [-Name] <String> [-Force] [-ProjectName <String>] [-StartUpProjectName <String>] 
  [-ConfigurationTypeName <String>] -ConnectionString <String> -ConnectionProviderName <String> 
  [-IgnoreChanges] [-AppDomainBaseDirectory <String>] [<CommonParameters>]

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

Update-Database [-SourceMigration <String>] [-TargetMigration <String>] [-Script] [-Force] 
  [-ProjectName <String>] [-StartUpProjectName <String>] [-ConfigurationTypeName <String>] 
  -ConnectionString <String> -ConnectionProviderName <String> 
  [-AppDomainBaseDirectory <String>] [<CommonParameters>]

Are there any good options to manage these commands within the project? I often find myself creating a .txt file with notes and saved command lines, and saving it in a "doc" folder at the solution level. I copy and paste commands from here as I need them, but it seems like there should be a better way to handle these commands.

Ideally, I'd like to be able to create a "batch file" (or something like it) for the Package Manager Console that can take simplified options and expand it to the full command. For example, I run Add-Migration-Ex MyNewMigration , and it gets expanded to the full Add-Migration command, with all my other standard options and switches for the current project applied.

Are there any easier ways to manage these types of complex commands that makes them easier to save and run on a per-project / solution level?

After doing a lot of research, I've found that NuGet has a system to run power-shell scripts at solution load. Nuget will look for a script named "init.ps1" in the package's "tools" directory, and execute it whenever the solution loads. I'm going to leverage this, and create a NuGet package that runs when the solution loads, that will check for a directory within the solution named "SolutionCommands". It will then run any scripts, and install any modules located within that directory. These scripts can be managed as part of the solution within Visual Studio, follow the solution around through version control, and automatically get distributed to all developers on the team.

I've created preliminary package that is available on Nuget now for anyone else who would like this functionality. It hasn't really been tested yet, and the scripts need to be signed (or the security policy of the computer relaxed) before they will run.


init.ps1:

$oldLocation = Get-Location

$oldWarningPref = $WarningPreference
$WarningPreference = 'SilentlyContinue'

$commandDirectory = ".\SolutionCommands"

If(Test-Path $commandDirectory)
{
    Write-Host "Installing Modules..."
    Get-ChildItem $commandDirectory -Filter "*.psm1" |
    ForEach-Object {
        $fullPath = Join-Path -Path $commandDirectory -ChildPath $_.Name
        $fileName = Split-Path -Path $fullPath -Leaf
        $fileNameNoExtension = [System.IO.Path]::GetFileNameWithoutExtension($fullPath)

        if (Get-Module | ?{ $_.Name -eq $fileNameNoExtension })
        {
            Remove-Module $fileNameNoExtension
        }
        Import-Module ($fullPath)

        Write-Host "Loaded module: " $fullPath
        Write-Host "      Commands: " (Get-Command -Module $fileNameNoExtension)
        Write-Host
    }

    Write-Host "Executing Scripts..."
    Get-ChildItem $commandDirectory -Filter "*.ps1" |
    ForEach-Object {
        $fullPath = Join-Path -Path $commandDirectory -ChildPath $_.Name
        Write-Host $fullPath
        . $fullPath
    }
}

Set-Location $oldLocation
$WarningPref = $oldWarningPref

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM