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

前端 未结 4 1121
余生分开走
余生分开走 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:06

    There is no equivalent of the query.ToString() AFAIK. You can eventually use DbContext.Database.Log property:

    db.Database.Log = s =>
    {
        // You can put a breakpoint here and examine s with the TextVisualizer
        // Note that only some of the s values are SQL statements
        Debug.Print(s);
    };
    db.SaveChanges();
    

提交回复
热议问题