我在哪里可以find在包pipe理器窗口中执行的代码的控制台或debugging输出?

我首先使用EntityFramework代码进行迁移。 从包pipe理器控制台,我正在运行“更新数据库”。 这将执行我已覆盖的Configuration.Seed(上下文)。

protected override void Seed(WebContext context) { Console.WriteLine("Console Test"); Debug.WriteLine("Debug Test"); Trace.WriteLine("Trace Test"); } 

我在哪里可以find输出?

更好的是,如何输出到包pipe理器窗口?

Thx,Dan

我在哪里可以find输出?

对不起,但是快速的回答基本上是没有的。

至less不要在包pipe理器控制台中。

 Debug.WriteLine("Debug Test"); Trace.WriteLine("Trace Test"); 

如果连接另一个Visual Studio来debugging正在运行update-database命令的Visual Studio实例,则可以看到Debug...Trace...方法的输出。 然后在debuggingVS中,您可以在输出窗口中看到输出。

 Console.WriteLine("Console Test"); 

如果使用EF附带的migrate.exe命令行工具运行迁移,则可以看到Console...方法的输出:

在这里输入图像说明

我如何输出回包pipe理器窗口?

我在这里也有一个坏消息,经过一个快速的“reflection”:在EF迁移的当前实现中,不支持在update-database (或任何其他命令)的执行过程中显示定制信息。

我使用的一个快速入门就是能够快速在我的Seed方法中find一个值,只不过是抛出一个具有我所关心的值的exception

 throw new Exception(yourValue); 

这种错误出种子,但我的例外/价值出现在我的包pipe理器控制台。

运行SQL打印命令将写入软件包pipe理器控制台。 这是我使用的一个辅助方法:

  /// <summary> /// write a message to the Package Manager Console /// </summary> public void Debug(string s, params object[] args) { var fullString = string.Format(s, args).Replace("'", "''"); Sql(string.Format("print '{0}'", fullString)); } 

我的需求与您的需求相似,所以我想我会在这里logging他们,以防他们可以帮助别人。 我的目标是显示迁移的所有输出,包括所有的sql run作为Seed方法的一部分。 作为此解决scheme的副作用,您还将能够在代码中看到任何Debug.Write消息。

首先创build一个DebugMigrationsLogger,将所有迁移输出写入Debug.WriteLine(感谢http://whiteknight.github.io/2013/01/26/efcodeonlymigrations.html ):

 public class DebugMigrationsLogger : System.Data.Entity.Migrations.Infrastructure.MigrationsLogger { public override void Info(string message) { Debug.WriteLine(message); } public override void Verbose(string message) { Debug.WriteLine(message); } public override void Warning(string message) { Debug.WriteLine("WARNING: " + message); } } 

接下来确保你的DbContext有一个DbMigrationsConfiguration的子类:

 public class MyDbMigrationsConfiguration : DbMigrationsConfiguration<MyDbContext> { public MyDbMigrationsConfiguration() { } protected override void Seed(MartusDb db) { //... } } 

接下来,将您的迁移作为按需unit testing运行,以便您的testing运行器可以捕获输出。 我的unit testing看起来像这样:

 public void MigrateDb_Test() { var config = new MyDbMigrationsConfiguration { AutomaticMigrationDataLossAllowed = true }; var migrator = new DbMigrator(config); var loggingDecorator = new MigratorLoggingDecorator(migrator, new DebugMigrationsLogger()); loggingDecorator.Update(); } 

最后,在你的DbContext构造函数中设置Database.Log:

 public class MyDbContext : DbContext { public MyDbContext() { Database.Log = message => Debug.WriteLine(message); } } 

现在,无论何时运行MigrateDb_Test(),您都将看到所有输出,这使得debugging迁移对我来说变得如此简单!

肮脏的解决方法延伸乔治的答案。

 protected override void Seed(YourContext context) { using (var seedout = new StringWriter()) { // do your work context.Authors.AddOrUpdate(x => x.Id, new Author() { Id = 1, Name = "Jane Austen" } ); // some message seedout.WriteLine("some message"); // commit your work context.SaveChanges(); seedout.WriteLine("Seed successfully completed."); // dummy exception to show message on package manager console throw new Exception(seedout.ToString()); } }