Find Out What Your Entity Framework Query Is Really Doing
Assuming that you're using Entity Framework 6, you already have a logging tool that can give you some insights into the SQL your queries are generating and the time they take to run. The Database property of your DbContext object has a Log property that will give you that information. To turn on logging, you just have to set the Log property to a method that will write to a log. This example sets up logging to write Entity Framework messages to to the Debug window:
Dim db As SalesOrderEntities
db = New SalesOrderEntities()
db.Database.Log = AddressOf Debug.WriteLine
In C#, you won't need the AddressOf keyword. You don't have to touch your code to make this change and you can also write your output to file by making some changes in your config file. To turn on logging to a file from your config file, just add a reference to the DatabaseLogger in the System.Data.Entity.Infrastructure.Interception namespace and point it to a file with XML, like this:
<interceptors>
<interceptor type=
"System.Data.Entity.Infrastructure.Interception.DatabaseLogger, EntityFramework">
<parameters>
<parameter value="C:\Logs\MyApplication.txt"/>
</parameters>
</interceptor>
</interceptors>
Here's what the output looks like (slightly edited):
Opened connection at 08-Nov-16 12:15:51 PM -05:00 SELECT TOP (1)
[c].[Id] AS [Id],
[c].[FirstName] AS [FirstName],
[c].[LastName] AS [LastName],
[c].[CustCreditStatus] AS [CustCreditStatus],
[c].[CreditLimit] AS [CreditLimit],
[c].[RenewalDate] AS [RenewalDate],
[c].[Valid] AS [Valid]
FROM [dbo].[Customers] AS [c]
-- Executing at 08-Nov-16 12:15:51 PM -05:00
-- Completed in 3 ms with result: SqlDataReader
Closed connection at 08-Nov-16 12:15:51 PM -05:00
when I used the First method on one of my tables.
Find Out What Your Entity Framework Query Is Really Doing的更多相关文章
- 第三篇 Entity Framework Plus 之 Query Cache
离上一篇博客,快一周,工作太忙,只能利用休息日来写一些跟大家分享,Entity Framework Plus 组件系列文章,之前已经写过两篇 第一篇 Entity Framework Plus 之 A ...
- 第二篇 Entity Framework Plus 之 Query Future
从性能的角度出发,能够减少 增,删,改,查,跟数据库打交道次数,肯定是对性能会有所提升的(这里单纯是数据库部分). 今天主要怎样减少Entity Framework查询跟数据库打交道的次数,来提高查询 ...
- Entity Framework 6.0 Tutorials(2):Async query and Save
Async query and Save: You can take advantage of asynchronous execution of .Net 4.5 with Entity Frame ...
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第二章:利用模型类创建视图、控制器和数据库
在这一章中,我们将直接进入项目,并且为产品和分类添加一些基本的模型类.我们将在Entity Framework的代码优先模式下,利用这些模型类创建一个数据库.我们还将学习如何在代码中创建数据库上下文类 ...
- 采用EntityFramework.Extended 对EF进行扩展(Entity Framework 延伸系列2)
前言 Entity Framework 延伸系列目录 今天我们来讲讲EntityFramework.Extended 首先科普一下这个EntityFramework.Extended是什么,如下: 这 ...
- 重新认识了下Entity Framework
什么是Entity Framework Entity Framework是一个对象关系映射O/RM框架. Entity Framework让开发者可以像操作领域对象(domain-specific o ...
- Entity Framework 6 Recipes 2nd Edition(9-4)译->Web API 的客户端实现修改跟踪
9-4. Web API 的客户端实现修改跟踪 问题 我们想通过客户端更新实体类,调用基于REST的Web API 服务实现把一个对象图的插入.删除和修改等数据库操作.此外, 我们想通过EF6的Cod ...
- Entity Framework 6 Recipes 2nd Edition(13-2)译 -> 用实体键获取一个单独的实体
问题 不管你用DBFirst,ModelFirst或是CodeFirst的方式,你想用实体键获取一个单独的实体.在本例中,我们用CodeFirst的方式. 解决方案 假设你有一个模型表示一个Paint ...
- Entity Framework 6 Recipes 2nd Edition(13-3)译 -> 为一个只读的访问获取实体
问题 你想有效地获取只是用来显示不会更新的操作的实体.另外,你想用CodeFirst的方式来实现 解决方案 一个非常常见行为,尤其是网站,就是只是让用户浏览数据.大多数情况下,用户不会更新数据.在这种 ...
随机推荐
- WPF 自定义CheckBox样式
自定义CheckBox样式,mark一下,方便以后参考复用 设计介绍: 1.一般CheckBox模板太难看了,肯定要重写其中的模板 2.模板状态为未选中状态和选中状态,设置为默认未选中就好了. 默认状 ...
- [转]C# 使用Nlog记录日志到数据库
本文转自:http://www.cnblogs.com/weixing/archive/2013/04/26/3044422.html 摘要]Nlog是一个很不错的.NET日志记录组件,它可以将日志输 ...
- Linux 系统常用命令汇总(三) 用户和用户组管理
用户和用户组管理 命令 选项 注解 示例 useradd [选项] 用户名 新建用户 创建一个名为tester的用户,并指定他的UID为555,指定加入test群,指定其使用C-shell: use ...
- VPN 部署方案
VPN 对比: OpenVPN: 客户端连接太麻烦,放弃 PPTP VPN:版本较高的苹果手机没有 PPTP VPN 的连接方式,放弃 L2TP VPN:支持所有平台,客户端连接容易,最终选择部署 L ...
- Linux ——————用Secure传文件时直接拖了文件用的是AssIC导致linux那边直乱码
如下: 解决办法: 直接删除.
- activity的启动模式
有四种启动模式:standard.singleTop.singleTask.singleInstance. 可在AndroidManifest.xml设置android:launchMode属性,如: ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Longest Absolute File Path 最长的绝对文件路径
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- 外网访问内网工具ngrok tunnel 使用总结
需求分析 在软件开发测试过程中,我们会经常遇到需要网站部署测试.给客户演示.APP开发的调试这样的需求.通常的做法是申请一个域名和空间,将网站放到外网上给客户演示. 这种方法确实可行不过会有两点不好, ...
- .NET WebAPI 用ActionFilterAttribute实现token令牌验证与对Action的权限控制
项目背景是一个社区类的APP(求轻吐...),博主主要负责后台业务及接口.以前没玩过webAPI,但是领导要求必须用这个(具体原因鬼知道),只好硬着头皮上了. 最近刚做完权限这一块,分享出来给大家.欢 ...