0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

EF6でコードからMigrationするときにログする方法

Posted at

概要

EF6でコードからMigrationするときに、Migrationの実行内容を見たいときがあります。
その方法です。

やること

1. MigrationsLoggerを継承したロギングクラスを作成します。以下はConsoleに出力する例です。

MyLogger.cs
    public class MyLogger : System.Data.Entity.Migrations.Infrastructure.MigrationsLogger
    {
        public override void Info(string message)
        {
            System.Console.WriteLine($"[Info]{message}");
        }

        public override void Verbose(string message)
        {
            System.Console.WriteLine($"[Verb]{message}");
        }

        public override void Warning(string message)
        {
            System.Console.WriteLine($"[Warn]{message}");
        }
    }

2. MigrationLoggingDecoratorを生成してUpdate()します。

Program.cs

        static void Main(string[] args)
        {
            var configuration = new Configuration(); // Enable-Migrationsで自動生成されたクラス
            var migrator = new System.Data.Entity.Migrations.DbMigrator(configuration)
            MigratorLoggingDecorator logger = new System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator(migrator, new MyLogger());
            logger.Update();
            :
            :

参考URL

0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?