首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在更新数据库期间,有任何方法显示输出吗?

在更新数据库期间,有任何方法显示输出吗?
EN

Stack Overflow用户
提问于 2014-03-05 19:27:47
回答 3查看 3.2K关注 0票数 7

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

我想显示其余的条目或百分比完成,或任何真正的。

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-03-05 19:36:05

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

--我将描述两个现成的选项:

Console.WriteLine 1. 解决方案:

无论是从控制台应用程序运行代码,还是从任何其他应用程序类型的Library (例如asp.net)运行代码,都可以使用Console.WriteLine函数,但主要问题是是否存在到Console输出的侦听器。

如果没有监听控制台,则利用Win32.AllocConsole() Windows函数来打开侦听器

代码语言:javascript
复制
using System.Runtime.InteropServices;
[DllImport("kernel32")]
static extern bool AllocConsole();

使用:

代码语言:javascript
复制
AllocConsole();
//now you can call: Console.WriteLine();
Console.WriteLine("Total number of records left to seed:{0}", total);

考虑用以下指令包装上述代码,以避免在生产环境中出现令人痛苦的意外:

代码语言:javascript
复制
#if DEBUG

#endif

Explanation:

AllocConsole初始化新控制台的标准输入、标准输出和标准错误句柄。标准输入句柄是控制台输入缓冲区的句柄,标准输出和标准错误句柄是控制台屏幕缓冲区的句柄。要检索这些句柄,请使用GetStdHandle函数。

Debug.Print 2. 解决方案:

在调试模式下使用Debug.Print函数。使用它,您可以将任何您想要的内容写入Visual的调试输出窗口:

调试-> Windows ->输出->显示来自-> Debug的输出

顺便问一下,您考虑过使用NLogLOG4NET将这些信息记录到某个文本文件中吗?

票数 7
EN

Stack Overflow用户

发布于 2015-09-11 14:29:23

如果我对您的理解是正确的,您需要添加标志‘-详细’来更新-数据库命令:

代码语言:javascript
复制
update-database -Verbose
票数 3
EN

Stack Overflow用户

发布于 2014-03-05 21:07:20

如果您可以从自定义应用程序中运行更新,则使用EF6.0拦截,这是一种恶意解决方案。

代码语言:javascript
复制
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)
        {
        }
    }
}

种子法:

代码语言:javascript
复制
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);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22207314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档