8

I'm running a fairly large seed as a stress test, and I'm wondering if it's possible to show output to a console.

I'd like to show the remaining entries or percent complete, or anything really.

Is there a way to write to the console or package manager console during update-database?

Charles W
  • 2,262
  • 3
  • 25
  • 38

3 Answers3

7

Is there a way to write to the console or package manager console during update-database?

I will describe two options which are available for you out of the box:

1. Console.WriteLine Solution:

Either if you're running your code from a Console application or from a Library of any other application type (e.g. asp.net), you can use the Console.WriteLine function, but the main question is if there is a listener to the Console output.

If nothing is listening to the Console, then take advantage of the Win32.AllocConsole() Windows APIs function in order to open a listener:

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

Usage:

AllocConsole();
//now you can call: Console.WriteLine();
Console.WriteLine("Total number of records left to seed:{0}", total);

Consider to wrap the above code with the following directive in order to avoid of painful surprises in the production environment:

#if DEBUG

#endif

Explanation:

AllocConsole initializes standard input, standard output, and standard error handles for the new console. The standard input handle is a handle to the console's input buffer, and the standard output and standard error handles are handles to the console's screen buffer. To retrieve these handles, use the GetStdHandle function.

2. Debug.Print Solution:

Use the Debug.Print function on debug mode. Using it, you would be able to write whatever you want to the DEBUG output window of Visual Studio:

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

By the way, did you consider logging this information using NLog or LOG4NET to some text file, for instance?

Yair Nevet
  • 12,725
  • 14
  • 66
  • 108
  • What do you mean by listening? I've tried running Console.WriteLine, however nothing appears when I run the seed from update-database. – Charles W Mar 05 '14 at 20:00
  • **listening**: You are writing to the Console, but the question if there is some open Console window to show your writes. – Yair Nevet Mar 05 '14 at 20:11
  • This worked great! Thank you! If anyone else is looking to do this in WPF like I was, check out the answer [here](http://stackoverflow.com/questions/160587/no-output-to-console-from-a-wpf-application) – Charles W Mar 07 '14 at 14:58
4

if i understand you right, you need to add flag '-Verbose' to update-database command:

update-database -Verbose
RustemMazitov
  • 958
  • 1
  • 10
  • 15
0

Kind of a hackish solution using EF 6.0 Interception if you can run your updates from a custom application.

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)
        {
        }
    }
}

Seed Method:

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);
}
Lorentz Vedeler
  • 5,101
  • 2
  • 29
  • 40