LoginSignup
2
4

More than 5 years have passed since last update.

EntityFrameWorkでSQLのログを出力する方法

Last updated at Posted at 2019-02-18

環境

Entity Framework Version 6

ログの出力指定

DbContextクラスを継承したクラスのコンストラクタにてログ出力の設定を行う。具体的はDbContextクラスのDatabaseプロパティのLogプロパティにログの出力方法を指定すればよい。

public partial class SampleModel : DbContext
    {
        public SampleModel(string connectionString)
            : base(connectionString)
        {
            this.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
        }

        public virtual DbSet<Sample> Samples { get; set; }
    }

上記のような設定を行うことでコマンドライン上にEntityFrameworkが実行したSQLのログを確認することができる。

2
4
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
2
4