Is there any way to show output during database update?

I use a fairly large seed as a stress test, and I wonder if output can be output to the console.

I would like to show the remaining records or percentages in full or something really.

Is there a way to write to the console or console of the package manager while updating the database?

+4
source share
3 answers

Is there a way to write a console or package manager console while updating the database?

I will describe two options that are available to you out of the box:

1. Console.WriteLineSolution:

(, asp.net), Console.WriteLine, , Console.

, Win32.AllocConsole() 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 , . - , . , GetStdHandle.

2. Debug.Print :

Debug.Print . , , , DEBUG Visual Studio:

→ Windows → → →

, NLog LOG4NET , ?

+4

, "-Verbose" update-database:

update-database -Verbose
+1

Kind of a hacker solution using EF 6.0 Interception, if you can run your updates from a special 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);
}
0
source

Source: https://habr.com/ru/post/1530300/


All Articles