简体   繁体   English

在项目中管理Entity Framework的程序包管理器控制台命令

[英]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-MigrationUpdate-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. 它还没有真正测试过,脚本需要在它们运行之前签名(或者计算机的安全策略放松)。


init.ps1: 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架程序包管理器控制台命令错误-生成失败 - Entity Framework Package Manager Console Commands Error - failed to build Project X 以框架“.NETStandard”为目标。 实体框架 Package 管理器控制台工具不支持此框架 - Project X targets framework '.NETStandard'. The Entity Framework Package Manager Console Tools don't support this framework 实体框架包管理器控制台问题 - Entity Framework Package Manager Console question 使用包管理器控制台“ Update-Database”等在实体框架中以编程方式连接字符串 - Programmatically connection string in entity framework with package manager console “Update-Database” etc 当我在 package 管理器控制台中的 Visual Studio 2013 中安装实体框架时 - When I am installing Entity Framework in Visual Studio 2013 in the package manager console 实体框架:在包管理器控制台VS 2012中添加迁移时抛出Null Reference Exception - Entity framework: Null Reference Exception thrown when adding migration in package manager console VS 2012 是否可以从 package 管理器控制台运行 angular 命令? - Is it possible to run angular commands from package manager console? 实体框架7,Visual Studio 2015,控制台命令不起作用 - Entity framework 7, visual studio 2015, console commands don't work 使用实体框架管理TypeList - Managing TypeLists w/ Entity Framework 实体框架:package 管理控制台中的启用迁移不起作用 - Entity Framework: enable-migrations in package management console does not work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM