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

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

问题描述

根据此线程,我们可以记录生成的 SQL 通过 EF ,但是如何 DbContext.SaveChanges()?有没有任何简单的方法来做这项工作,没有任何额外的框架?

According this thread, we can log the generated SQL via EF, but what about DbContext.SaveChanges()? Is there any easy way to do this job without any extra frameworks?

推荐答案

在实体框架6.0中,Database类有一个属性动作<串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;

对于更高级的需求,您可以设置一个拦截。关于实体框架的更多信息维基

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

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

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