[英]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. 在处理具有“非标准”构建的解决方案时,在包管理器控制台中输入的实体框架命令(如Add-Migration
和Update-Database
可能会变得相当长且复杂。
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. 我经常发现自己创建一个带有注释和保存命令行的.txt文件,并将其保存在解决方案级别的“doc”文件夹中。 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. 理想情况下,我希望能够为Package Manager控制台创建一个“批处理文件”(或类似的东西),它可以采用简化的选项并将其扩展为完整的命令。 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. 例如,我运行Add-Migration-Ex MyNewMigration
,它将扩展为完整的Add-Migration
命令,并应用当前项目的所有其他标准选项和开关。
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有一个系统可以在解决方案负载下运行power-shell脚本。 Nuget will look for a script named "init.ps1" in the package's "tools" directory, and execute it whenever the solution loads. Nuget将在包的“tools”目录中查找名为“init.ps1”的脚本,并在解决方案加载时执行它。 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". 我将利用它,并创建一个在解决方案加载时运行的NuGet包,它将检查名为“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. 这些脚本可以作为Visual Studio中解决方案的一部分进行管理,通过版本控制遵循解决方案,并自动分发给团队中的所有开发人员。
I've created preliminary package that is available on Nuget now for anyone else who would like this functionality. 我已经为Nuget创建了初步包 ,现在可以为任何想要这个功能的人提供。 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. 它还没有真正测试过,脚本需要在它们运行之前签名(或者计算机的安全策略放松)。
$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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.