我的具体问题是如何在 Entity Framework 的构建过程中自动化“添加迁移”。在研究这一点时,似乎最有可能的方法是将这些步骤自动化
这种初始方法基于我自己的研究和 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 。但不想这样做,因为这会使围绕构建过程的自动化大大复杂化。
说了这么多,
提前感谢您的任何建议,建议,帮助,非滥用,
约翰
最佳答案
对我有用的方法是从 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/