visual-studio - 如何在 Visual Studio 2013 中自动化包管理器控制台

标签 visual-studio entity-framework nuget build-process

我的具体问题是如何在 Entity Framework 的构建过程中自动化“添加迁移”。在研究这一点时,似乎最有可能的方法是将这些步骤自动化

  • 在 Visual Studio 2013 中打开解决方案
  • 在包管理器控制台中执行“Add-Migration blahblah”(最有可能通过加载项 vsextention)
  • 关闭解决方案

  • 这种初始方法基于我自己的研究和 this question ,最终支持 Add-Migration 的 powershell 脚本需要相当多的设置才能运行。 Visual Studio 在创建包管理器控制台并使 DTE 对象可用时自动执行该设置。我不想尝试在 Visual Studio 之外复制该设置。

    解决方案的一种可能途径是这种 Unresolved 堆栈溢出 question

    在研究 NuGet API 时,它似乎没有“发送此文本,它将像在控制台中键入一样运行”。我不清楚 Visual Studio 与 NuGet 之间的界限,所以我不确定这是否会存在。

    我能够通过包管理器控制台中的“$dte.Windows”命令找到“包管理器控制台”,但在 VS 2013 窗口中,该集合为我提供了“Microsoft.VisualStudio.Platform.WindowManagement.DTE”对象.WindowBase”。如果有一种方法可以将文本填充到其中,我想我需要通过查看 source code 使其成为 NuGetConsole.Implementation.PowerConsoleToolWindow”| 我不清楚文本将如何填充,但我一点也不熟悉我在看。

    最坏的情况是,我将回过头来尝试按照 this question 的方式向其填充 key 。但不想这样做,因为这会使围绕构建过程的自动化大大复杂化。

    说了这么多,
  • 是否可以通过代码将命令流式传输到 Visual Studio 中的包管理器控制台,该控制台已完全初始化并能够支持 Entity Framework “添加迁移”命令?

  • 提前感谢您的任何建议,建议,帮助,非滥用,

    约翰

    最佳答案

    对我有用的方法是从 EntityFramework.Powershell 项目中的 AddMigrationCommand.cs 开始跟踪 Entity Framework 代码,找到 EntityFramework 项目中的 Hook ,然后使这些 Hook 工作,因此没有 Powershell 依赖项。

    你可以得到类似...

        public static void RunIt(EnvDTE.Project project, Type dbContext, Assembly migrationAssembly, string migrationDirectory,
            string migrationsNamespace, string contextKey, string migrationName)
        {
            DbMigrationsConfiguration migrationsConfiguration = new DbMigrationsConfiguration();
            migrationsConfiguration.AutomaticMigrationDataLossAllowed = false;
            migrationsConfiguration.AutomaticMigrationsEnabled = false;
            migrationsConfiguration.CodeGenerator = new CSharpMigrationCodeGenerator(); //same as default
            migrationsConfiguration.ContextType = dbContext; //data
            migrationsConfiguration.ContextKey = contextKey;
            migrationsConfiguration.MigrationsAssembly = migrationAssembly;
            migrationsConfiguration.MigrationsDirectory = migrationDirectory;
            migrationsConfiguration.MigrationsNamespace = migrationsNamespace;
    
            System.Data.Entity.Infrastructure.DbConnectionInfo dbi = new System.Data.Entity.Infrastructure.DbConnectionInfo("DataContext");
            migrationsConfiguration.TargetDatabase = dbi;
    
            MigrationScaffolder ms = new MigrationScaffolder(migrationsConfiguration);
    
            ScaffoldedMigration sf = ms.Scaffold(migrationName, false);
    
        }
    

    您可以使用 this question到达 dte 对象并从那里找到要传递到调用中的项目对象。

    关于visual-studio - 如何在 Visual Studio 2013 中自动化包管理器控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20586367/

    相关文章:

    visual-studio - 无法在 VS2012 中更新或卸载 NuGet 包管理器

    c++ - findContours在opencv中找不到轮廓

    visual-studio - Azure DevOps 构建失败警告 MSB3245 : Could not resolve this reference

    c# - 什么相当于 Visual Studio 2010 中的 Visual Studio 2008 对象测试台?

    c# - 在 Entity Framework 中动态传递 Select for projection 中的属性

    mysql - EF Code First Migrations - 两个程序集 - 查看错误的配置文件

    asp.net - 为什么我不能使用 NuGet 删除包?

    c# - 检索 Entity Framework 中的存储过程输出参数始终为空

    c# - 如何在更改存储库路径后重置 HintPath

    visual-studio - 如何在 .NET Core 3.1 中使用更新版本的 NuGet 包作为依赖包?