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的更多相关文章

  1. 第三篇 Entity Framework Plus 之 Query Cache

    离上一篇博客,快一周,工作太忙,只能利用休息日来写一些跟大家分享,Entity Framework Plus 组件系列文章,之前已经写过两篇 第一篇 Entity Framework Plus 之 A ...

  2. 第二篇 Entity Framework Plus 之 Query Future

    从性能的角度出发,能够减少 增,删,改,查,跟数据库打交道次数,肯定是对性能会有所提升的(这里单纯是数据库部分). 今天主要怎样减少Entity Framework查询跟数据库打交道的次数,来提高查询 ...

  3. 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 ...

  4. ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第二章:利用模型类创建视图、控制器和数据库

    在这一章中,我们将直接进入项目,并且为产品和分类添加一些基本的模型类.我们将在Entity Framework的代码优先模式下,利用这些模型类创建一个数据库.我们还将学习如何在代码中创建数据库上下文类 ...

  5. 采用EntityFramework.Extended 对EF进行扩展(Entity Framework 延伸系列2)

    前言 Entity Framework 延伸系列目录 今天我们来讲讲EntityFramework.Extended 首先科普一下这个EntityFramework.Extended是什么,如下: 这 ...

  6. 重新认识了下Entity Framework

    什么是Entity Framework Entity Framework是一个对象关系映射O/RM框架. Entity Framework让开发者可以像操作领域对象(domain-specific o ...

  7. Entity Framework 6 Recipes 2nd Edition(9-4)译->Web API 的客户端实现修改跟踪

    9-4. Web API 的客户端实现修改跟踪 问题 我们想通过客户端更新实体类,调用基于REST的Web API 服务实现把一个对象图的插入.删除和修改等数据库操作.此外, 我们想通过EF6的Cod ...

  8. Entity Framework 6 Recipes 2nd Edition(13-2)译 -> 用实体键获取一个单独的实体

    问题 不管你用DBFirst,ModelFirst或是CodeFirst的方式,你想用实体键获取一个单独的实体.在本例中,我们用CodeFirst的方式. 解决方案 假设你有一个模型表示一个Paint ...

  9. Entity Framework 6 Recipes 2nd Edition(13-3)译 -> 为一个只读的访问获取实体

    问题 你想有效地获取只是用来显示不会更新的操作的实体.另外,你想用CodeFirst的方式来实现 解决方案 一个非常常见行为,尤其是网站,就是只是让用户浏览数据.大多数情况下,用户不会更新数据.在这种 ...

随机推荐

  1. 关于 IIS 中 Excel 访问的问题

    关于 IIS 上 Excel 文件的访问, 一路上困难重重, 最后按以下步骤进行设置, 可在 IIS 中正常使用! 1. 引用及代码: 1). 项目中添加 Excel 程序集引用(注意: 从系统 CO ...

  2. JS--遍历对象

    var person = { Name:"Frank", Age:23 } Object.keys(person).forEach(function(key){ console.l ...

  3. jdbc java数据库连接 7)获取插入数据的自增长值

    我们创建一个sql表,里面的数据往往都会有自增长值. 那么,我们用jdbc插入数据的时候,要想同时获得这个增长值. 代码: /** * * 这是插入一条数据的同时,获取该数据的则增长列的值(该例子的自 ...

  4. [LeetCode] Move Zeroes 移动零

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  5. [LeetCode] Expression Add Operators 表达式增加操作符

    Given a string that contains only digits 0-9 and a target value, return all possibilities to add ope ...

  6. 自动判断应该Ajax还是return

    起因 最近回顾以前的代码,发现一个偶尔会见到的现象.一个类里面的方法可能需要Ajax返回,也有可能需要函数return.这个现象发生在网站MVC中的 逻辑层(或模型层),示例如下.IndexCtrl是 ...

  7. jquery网页可见区域宽

    网页可见区域宽:document.body.clientWidth 网页可见区域高:document.body.clientHeight 网页可见区域宽:document.body.offsetWid ...

  8. [基础] Array.prototype.indexOf()查询方式

    背景 最近在看Redux源码,createStore用于注册一个全局store,其内部维护一个Listeren数组,存放state变化时所有的响应函数. 其中store.subscribe(liste ...

  9. windows下安装kibana出 "EPERM: operation not permitted

    D:\kibana-\bin>kibana-plugin install file:///x-pack-5.0.0.zip Attempting to transfer from file:// ...

  10. python3使用pyinstaller打包apscheduler出的错

    本来只是想用Python做一个定时任务小工具在服务器上运行,可是服务器在隔离区,各种禁止上外网,使用pip导出列表那种下载库的方法不管用,导致Python的各种库都下不到,官网离线下载又各种缺依赖,好 ...