How do I view the SQL generated by the Entity Framework?

后端 未结 22 2830
别那么骄傲
别那么骄傲 2020-11-21 05:35

How do I view the SQL generated by entity framework ?

(In my particular case I\'m using the mysql provider - if it matters)

22条回答
  •  不思量自难忘°
    2020-11-21 06:30

    For those using Entity Framework 6 and up, if you want to view the output SQL in Visual Studio (like I did) you have to use the new logging/interception functionality.

    Adding the following line will spit out the generated SQL (along with additional execution-related details) in the Visual Studio output panel:

    using (MyDatabaseEntities context = new MyDatabaseEntities())
    {
        context.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
        // query the database using EF here.
    }
    

    More information about logging in EF6 in this nifty blog series: http://blog.oneunicorn.com/2013/05/08/ef6-sql-logging-part-1-simple-logging/

    Note: Make sure you are running your project in DEBUG mode.

提交回复
热议问题