【问题标题】:Visual Studio - Package Manager Console - Running commands programmaticallyVisual Studio - 包管理器控制台 - 以编程方式运行命令
【发布时间】:2012-08-23 15:28:59
【问题描述】:

每次更新生产网站时,我需要运行 5 个命令,它们只是名称不同。

我想自动执行此操作(例如,将来我可能必须运行这些命令 100 次)。

我做的是:

Update-Database -ConfigurationTypeName "SlaveConfiguration" -StartupProjectName "FacturatieMVCv2.Data" -Verbose -ConnectionString "Server=PC-1\SQLEXPRESS;Persist Security Info=True;Initial Catalog=Database1;uid=prog;password=ndp103@50;" -ConnectionProviderName "System.Data.SqlClient" 
Update-Database -ConfigurationTypeName "SlaveConfiguration" -StartupProjectName "FacturatieMVCv2.Data" -Verbose -ConnectionString "Server=PC-1\SQLEXPRESS;Persist Security Info=True;Initial Catalog=Database2;uid=prog;password=ndp103@50;" -ConnectionProviderName "System.Data.SqlClient" 
Update-Database -ConfigurationTypeName "SlaveConfiguration" -StartupProjectName "FacturatieMVCv2.Data" -Verbose -ConnectionString "Server=PC-1\SQLEXPRESS;Persist Security Info=True;Initial Catalog=Database3;uid=prog;password=ndp103@50;" -ConnectionProviderName "System.Data.SqlClient" 
Update-Database -ConfigurationTypeName "SlaveConfiguration" -StartupProjectName "FacturatieMVCv2.Data" -Verbose -ConnectionString "Server=PC-1\SQLEXPRESS;Persist Security Info=True;Initial Catalog=Database4;uid=prog;password=ndp103@50;" -ConnectionProviderName "System.Data.SqlClient" 
Update-Database -ConfigurationTypeName "SlaveConfiguration" -StartupProjectName "FacturatieMVCv2.Data" -Verbose -ConnectionString "Server=PC-1\SQLEXPRESS;Persist Security Info=True;Initial Catalog=Database5;uid=prog;password=ndp103@50;" -ConnectionProviderName "System.Data.SqlClient" 

如何编写 i 脚本,这样,当我在控制台管理器中键入时:

Update-Database -type Production -version latest

它运行上面的所有命令,没有任何干扰。 (所有连接字符串都位于一个 .XML 文件中,例如:

 <databases>
    <type>Production
        <database>Database1</database>
        <database>Database2</database>
        <database>Database3</database>
        <database>Database4</database>
        <database>Database5</database>
    </type>
 </databases>

【问题讨论】:

  • 因为包管理器控制台是一个 PowerShell 主机,您可以为您的逻辑编写一个 PowerShell 脚本。
  • 我在问题中使用了 Powershell 作为标签,但我忘记在我的问题中提及 PowerShell...

标签: .net visual-studio-2010 powershell package-managers


【解决方案1】:

如果我正确理解您的问题,您只是在询问如何从包管理器控制台中编写和执行 powershell 脚本。

1) 用记事本在当前目录新建脚本:

notepad newScript.ps1

2) 将命令粘贴到记事本中

Update-Database...
Update-Datebase...
Update-Database...

3) 保存记事本文件

4) 通过在包管理器控制台中键入以下内容来执行记事本文件:

.\newScript.ps1

请注意,powershell 函数和参数的使用超出了此答案的范围。

【讨论】:

  • 执行“newScript.ps1”后出现这个错误:The term 'Update-Database' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.如何解决?
  • Entity Framework的包找不到(来晚了,不好意思)
【解决方案2】:

我对包管理器控制台本身一无所知,但如果目标是从 XML 中读取一堆数据库名称,将它们弹出到连接字符串中,然后将它们传递给 cmdlet,下面应该带你去:

$config = [xml](gc c:\path\to\config.xml)
$dbNames= $config.Databases.Type.Database

$dbNames|%{
  $connStr = "Server=PC-1\SQLEXPRESS;Persist Security Info=True;Initial Catalog=$_;uid=prog;password=ndp103@50;"
  Update-Database -ConfigurationTypeName "SlaveConfiguration" -StartupProjectName "FacturatieMVCv2.Data" -Verbose -ConnectionString $connStr -ConnectionProviderName "System.Data.SqlClient"
}

【讨论】:

  • 我使用了这个解决方案。这是最接近我想要的。它有效,谢谢!
  • @latkin :“我对包管理器控制台本身一无所知”,包管理器控制台是 Visual Studio 中的一个 PowerShell 控制台,用于与 NuGet 交互和自动化 Visual Studio。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-21
  • 1970-01-01
  • 2020-09-05
  • 2021-08-21
  • 2016-09-10
相关资源
最近更新 更多