Dan*_*Eli 45 entity-framework ef-code-first nuget ef-migrations
我首先使用EntityFramework代码进行迁移.从包管理器控制台,我正在运行"update-database".这会执行我已覆盖的Configuration.Seed(context).
    protected override void Seed(WebContext context)
    {
        Console.WriteLine("Console Test");
        Debug.WriteLine("Debug Test");
        Trace.WriteLine("Trace Test");
    }
我在哪里可以找到输出?
更好的是,如何输出回包管理器窗口?
谢,丹
Geo*_*ton 46
我用来快速找到我的Seed方法中的值的快速黑客只是抛出一个我关心的值的异常,例如
throw new Exception(yourValue);
这会导致种子错误,但我的异常/值会出现在我的包管理器控制台中.
nem*_*esv 34
我在哪里可以找到输出?
对不起,但快速答案基本上没有.
确切地说,至少不在包管理器控制台中.
Debug.WriteLine("Debug Test");
Trace.WriteLine("Trace Test");
如果附加另一个Visual Studio以调试运行该命令的Visual Studio实例,则可以看到Debug...和Trace...方法的输出update-database.然后在调试器VS中,您可以在输出窗口中看到输出.
Console.WriteLine("Console Test");
Console...如果migrate.exe使用EF附带的命令行工具运行迁移,则可以看到方法 
 的输出:

如何输出回包管理器窗口?
在快速"反思"之后,我在这里也有坏消息:在EF迁移的当前实现中,不支持在执行update-database(或任何其他命令)期间显示自定义信息.
jhi*_*den 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));
    }
我的需求与你的相似,所以我想我会在这里记录下来,以防他们可以帮助别人.我的目标是显示迁移的所有输出,包括作为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(),您都会看到所有输出,它使我的调试迁移变得更加容易!
| 归档时间: | 
 | 
| 查看次数: | 12873 次 | 
| 最近记录: |