我如何才能登录从DbContext.SaveChanges()在我的程序生成的SQL? [英] How can I log the generated SQL from DbContext.SaveChanges() in my Program?

查看:223
本文介绍了我如何才能登录从DbContext.SaveChanges()在我的程序生成的SQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

依据本主题: http://stackoverflow.com/a/1412902/1594487 我们可以记录生成的SQL通过EF,但对于 DbContext.SaveChanges()?有没有简单的方法来做到这一点,而无需使用一些额外的框架?

According this thread: http://stackoverflow.com/a/1412902/1594487 we can log the generated SQL via EF, but what about DbContext.SaveChanges()? Is there any easy way to do it without using some extra frameworks?

推荐答案

在实体框架6.0,数据库类有一个属性动作<串GT;登录。因此,在设置日志一样简单:

In entity framework 6.0, the Database class has a property Action<string> Log. so setting up logging is as easy as:

context.Database.Log = Console.WriteLine;

有关更高级的需求,可以设置一个<一个href=\"http://entityframework.$c$cplex.com/SourceControl/latest#src/EntityFramework/Infrastructure/Interception/IDbCommandInterceptor.cs\">interceptor.对实体框架的更多信息维基

For more advanced needs you can set up an interceptor. More info on the entity framework wiki

这篇关于我如何才能登录从DbContext.SaveChanges()在我的程序生成的SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆