C# 在更新数据库期间,是否有任何方法显示输出?

C# 在更新数据库期间,是否有任何方法显示输出?,c#,entity-framework,C#,Entity Framework,我正在运行一个相当大的seed作为压力测试,我想知道是否可以将输出显示到控制台 我想显示剩余的条目或完成百分比,或者其他任何内容 在更新数据库期间是否有写入控制台或包管理器控制台的方法 是否有写入控制台或包管理器控制台的方法 在更新数据库期间 我将介绍两个现成的选项: AllocConsole(); //now you can call: Console.WriteLine(); Console.WriteLine("Total number of records left to seed:{0

我正在运行一个相当大的seed作为压力测试,我想知道是否可以将输出显示到控制台

我想显示剩余的条目或完成百分比,或者其他任何内容

在更新数据库期间是否有写入控制台或包管理器控制台的方法

是否有写入控制台或包管理器控制台的方法 在更新数据库期间

我将介绍两个现成的选项:

AllocConsole();
//now you can call: Console.WriteLine();
Console.WriteLine("Total number of records left to seed:{0}", total);
1<代码>控制台.写入线解决方案:

AllocConsole();
//now you can call: Console.WriteLine();
Console.WriteLine("Total number of records left to seed:{0}", total);
如果您是从控制台应用程序或任何其他应用程序类型(如asp.net)的运行代码,则可以使用
控制台.WriteLine
函数,但主要问题是
控制台
输出是否有侦听器

如果没有任何内容正在侦听控制台,则利用Win32.alloconsole()的
Windows API函数打开一个侦听器

using System.Runtime.InteropServices;
[DllImport("kernel32")]
static extern bool AllocConsole();
用法:

AllocConsole();
//now you can call: Console.WriteLine();
Console.WriteLine("Total number of records left to seed:{0}", total);
考虑使用以下指令包装上述代码,以避免在生产环境中出现令人痛苦的意外:

#if DEBUG

#endif
说明:

AllocConsole();
//now you can call: Console.WriteLine();
Console.WriteLine("Total number of records left to seed:{0}", total);
AllocConsole初始化标准输入、标准输出和标准输出 新控制台的错误句柄。标准输入句柄是一个 控制台输入缓冲区的句柄,以及标准输出和 标准错误句柄是控制台屏幕缓冲区的句柄。到 要检索这些句柄,请使用GetStdHandle函数

2<代码>调试。打印
解决方案:

AllocConsole();
//now you can call: Console.WriteLine();
Console.WriteLine("Total number of records left to seed:{0}", total);
在调试模式下使用
Debug.Print
功能。使用它,您可以在Visual Studio的“调试输出”窗口中编写任何需要的内容:

Debug->Windows->Output->Show Output from->Debug


顺便问一下,您是否考虑过使用或使用某个文本文件来记录这些信息,例如,

< P>一种使用EF 6拦截的哈希解决方案,如果您可以从自定义应用程序运行更新。
public class Program
{
    static void Main(string[] args)
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<DatabaseEntities, Configuration>());
        using (var entities = new DatabaseEntities())
        {
            entities.Database.Initialize(true);
        }
        Console.Read();
    }

    public class ProgressInterceptor : IDbCommandInterceptor
    {
        private int currentRecord = 0;
        public int TotalCount { get; set; }

        public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
        {
        }

        public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
        {
        }

        public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
        {
            Console.WriteLine("Progress {0:P}", ++currentRecord / (double)TotalCount);
        }

        public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
        {
        }

        public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
        {
        }

        public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
        {
        }
    }
}
公共类程序
{
静态void Main(字符串[]参数)
{
SetInitializer(新的MigrateDatabaseToLatestVersion());
使用(var entities=newdatabaseentities())
{
entities.Database.Initialize(true);
}
Console.Read();
}
公共类ProgressInterceptor:IDbCommandInterceptor
{
私有int currentRecord=0;
公共整数TotalCount{get;set;}
public void NonQueryExecuted(DbCommand命令,DbCommandInterceptionContext interceptionContext)
{
}
public void非查询执行(DbCommand命令,DbCommandInterceptionContext interceptionContext)
{
}
public void ReaderExecuted(DbCommand命令,DbCommandInterceptionContext interceptionContext)
{
WriteLine(“Progress{0:P}”,++currentRecord/(double)TotalCount);
}
public void reader执行(DbCommand命令,DbCommandInterceptionContext interceptionContext)
{
}
已执行公共空标度(DbCommand命令、DbCommandInterceptionContext interceptionContext)
{
}
public void scalare执行(DbCommand命令、DbCommandInterceptionContext interceptionContext)
{
}
}
}
种子法:

protected override void Seed(EfProgress.DatabaseEntities context)
{
    var interceptor = new EfProgress.Program.ProgressInterceptor() { TotalCount = 10 };
    DbInterception.Add(interceptor);
    for (int i = 0; i < 10; i++)
    {
        var entity = context.EntitySet.Create();
        entity.Property1 = "entity " + i;
        context.EntitySet.Add(entity);
    }

    context.SaveChanges();

    DbInterception.Remove(interceptor);
}
protected override void Seed(EfProgress.DatabaseEntities上下文)
{
var interceptor=new EfProgress.Program.ProgressInterceptor(){TotalCount=10};
添加(拦截器);
对于(int i=0;i<10;i++)
{
var entity=context.EntitySet.Create();
entity.Property1=“entity”+i;
context.EntitySet.Add(实体);
}
SaveChanges();
删除(拦截器);
}

如果我理解正确,您需要添加标志“-Verbose”以更新数据库命令:

update-database -Verbose

你的应用程序输出类型是库还是控制台?我的应用程序是库。很有趣,你做到了吗?你说的监听是什么意思?我已经尝试运行Console.WriteLine,但是当我从update-database运行seed时,什么也没有出现。正在侦听:您正在向控制台写入内容,但问题是是否有打开的控制台窗口显示您的写入内容。这非常有效!非常感谢。如果其他人希望像我一样在WPF中这样做,请查看答案
指定“-Verbose”标志以查看应用于目标数据库的SQL语句。
如何将此输出保存在文件中