Entity Framework 6 - How can I view the SQL that will be generated for an insert before calling SaveChanges

前端 未结 4 910
余生分开走
余生分开走 2021-02-02 08:19

In Entity Framework 6, is it possible to view the SQL that will be executed for an insert before calling SaveChanges?

using (var db =          


        
4条回答
  •  鱼传尺愫
    2021-02-02 09:01

    The easiest way in EF6 To have the query always handy, without changing code is to add this to your DbContext and then just check the query on the output window in visual studio, while debugging.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        Database.Log = (query)=> Debug.Write(query);
    }
    

    EDIT

    LINQPad is also a good option to debug Linq with and can also show the SQL queries.

提交回复
热议问题