Find Out What Your Entity Framework Query Is Really Doing
2017-01-05 11:15
337 查看
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:
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:
Here's what the output looks like (slightly edited):
when I used the First method on one of my tables.
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.
相关文章推荐
- 什么是游戏开发的实体系统框架 What is an entity system framework for game development
- What is Entity Framework?
- What is Entity Framework?
- What is an entity system framework for game development?
- Find out what your UNIX shell’s flags are & then change them
- 什么是游戏开发的实体系统框架 What is an entity system framework for game development
- [转]How Can I Find Out What Is Using a Busy or Reserved Serial Port?
- without inq or pp how do you find out what LUN / array a scsi device is
- 【转】What is an entity system framework for game development?
- Entity Framework Tutorial Basics(2):What is Entity Framework?
- Entity Framework Code-First(2):What is Code-First?
- Xcode4 布置Git环境Your working copy is out of date. Try pulling from the remote to get the latest change
- 杭电acm 1084 What Is Your Grade? 比较考人的简单题
- Know Your Android Bootloader—What it is and Why it Matters
- What is Favicon.ico and How to Create a Favicon Icon for Your Website?[zz]
- HDU 1084 What Is Your Grade?(排序)
- What is entity relationship diagram? - Definition from WhatIs.com
- what is SW framework
- “TableDetails”中列“IsPrimaryKey”的值为DBNull. Mysql EntityFramework
- What is framework?