实体框架 4.1 - EFTracingProvider
Is there a way to make the EFTracing provider work with EF 4.1?
EFTracing seems to need an objectcontext and I use dbcontext.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,你可以。我正在使用 社区版本 以及数据库优先 DbContext 和代码优先 DbContext。此答案基于项目网站上的讨论主题。
对于数据库优先/设计器 DbContext(通过 ADO.NET DbContext 生成器模板),您可以简单地添加以下构造函数:
对于代码优先的 DbContexts,它有点复杂,因为 EFTracingProvider 需要完整的实体连接字符串。您必须手动创建 EFTracingConnection 的实例。以下示例适用于数据库优先和代码优先上下文。
Yes, you can. I'm using the community version with both database-first DbContexts and code-first DbContexts. This answer is based on a discussion thread on the project site.
For database-first/designer DbContexts (via ADO.NET DbContext Generator templates), you can simply add the following constructor:
For code first DbContexts its a bit more complicated since the EFTracingProvider wants a full entity connection string. You have to create an instance of EFTracingConnection manually. The following example will work for both database first and code first contexts.
虽然前面的答案有效,但我发现它们存在问题,一个更简单的解决方案是使用 NuGet 中的 Clutch.Diagnostics.EntityFramework 包,该包在幕后使用 MiniProfiler。与 EFTracingProvider 相比,它的使用起来要简单得多,并且是一个更加灵活的解决方案。
该项目位于 GitHub 上 https://github.com/Kukkimonsuta/Clutch
对于 EFTracingProvider 之类的功能,请安装 NuGet 包,然后实施IDbTracingListener 像这样:
Whilst the previous answers work, I've found them problematic, a much simpler solution is to use the Clutch.Diagnostics.EntityFramework package from NuGet that uses MiniProfiler behind the scenes. It is significantly simpler to get working than EFTracingProvider, and a much more flexible solution.
The project is on GitHub at https://github.com/Kukkimonsuta/Clutch
For EFTracingProvider like functionality install the NuGet package and then implement IDbTracingListener like this:
如果您使用 DBContext 和 MVC Model First,即使用 EntityConnections,那么您只需要以下示例代码:
另外:
在 Web.Config 文件中添加以下部分:
并且:
无需包含原文章中提到的 ExtendedEntities 或其他 ObjectContext 派生类。运行该代码,您应该会看到指定的日志文件,其中包含所有 SQL 命令。当启用跟踪时,我绕过数据库初始化,
If you are using DBContext and MVC Model First, meaning using EntityConnections then the following sample code should be all you need:
Also:
In the Web.Config file add the following sections:
and:
There is no need to include the ExtendedEntities or the other ObjectContext derived class mentioned in the original article. Run that code and you should see your log file as specified, with all the SQL commands in it. I am bypassing database intialization when tracing is enabled,