46

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

    protected override void Seed(WebContext context)
    {

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

我在哪里可以找到该输出?

更好的是,如何输出回包管理器窗口?

谢谢,丹

4

5 回答 5

48

我用来在我的 Seed 方法中快速找到一个值的快速技巧就是抛出一个我关心的值的异常,例如

throw new Exception(yourValue);

这会使种子出错,但我的异常/值出现在我的包管理器控制台中。

于 2013-03-12T16:47:25.100 回答
35

我在哪里可以找到该输出?

对不起,但快速回答基本上无处可去。

确切地说,至少不在包管理器控制台中。

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

如果您附加另一个 Visual Studio 来调试正在运行该命令的 Visual Studio 实例,则可以看到Debug...和方法的输出。然后在调试 VS 中,您可以在 Output Window 中看到输出。Trace...update-database

Console.WriteLine("Console Test");

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

在此处输入图像描述

如何输出回包管理器窗口?

在快速“反思”之后,我在这里也有一个坏消息:在 EF 迁移的当前实现中,不支持在执行update-database(或任何其他命令)期间显示自定义信息。

于 2012-03-30T19:28:32.200 回答
17

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

    /// <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));
    }
于 2014-10-28T21:51:57.560 回答
8

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

首先创建一个 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)
    {
        //...
    }
}

接下来,您将迁移作为按需单元测试运行,以便您的测试运行程序可以捕获输出。我的单元测试看起来像这样:

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() 时,您都会看到所有输出,它使调试迁移对我来说变得非常容易!

于 2015-05-01T16:40:44.323 回答
1

肮脏的解决方法扩展了乔治的答案。

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());
    }
}
于 2016-06-17T08:21:29.917 回答