我正在运行一个相当大的种子作为压力测试,我想知道是否有可能将输出显示到控制台。
我想显示其余的条目或百分比完成,或任何真正的。
是否有方法在更新数据库期间写入控制台或包管理器控制台?
发布于 2014-03-05 19:36:05
是否有方法在更新数据库期间写入控制台或包管理器控制台?
--我将描述两个现成的选项:
Console.WriteLine 1. 解决方案:
无论是从控制台应用程序运行代码,还是从任何其他应用程序类型的Library (例如asp.net)运行代码,都可以使用Console.WriteLine函数,但主要问题是是否存在到Console输出的侦听器。
如果没有监听控制台,则利用Win32.AllocConsole() Windows函数来打开侦听器
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
#endifExplanation:
AllocConsole初始化新控制台的标准输入、标准输出和标准错误句柄。标准输入句柄是控制台输入缓冲区的句柄,标准输出和标准错误句柄是控制台屏幕缓冲区的句柄。要检索这些句柄,请使用GetStdHandle函数。
Debug.Print 2. 解决方案:
在调试模式下使用Debug.Print函数。使用它,您可以将任何您想要的内容写入Visual的调试输出窗口:
调试-> Windows ->输出->显示来自-> Debug的输出
顺便问一下,您考虑过使用NLog或LOG4NET将这些信息记录到某个文本文件中吗?
发布于 2015-09-11 14:29:23
如果我对您的理解是正确的,您需要添加标志‘-详细’来更新-数据库命令:
update-database -Verbose发布于 2014-03-05 21:07:20
如果您可以从自定义应用程序中运行更新,则使用EF6.0拦截,这是一种恶意解决方案。
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)
{
}
}
}种子法:
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);
}https://stackoverflow.com/questions/22207314
复制相似问题