Find Your Entity Framework Query is really Doing

Source: Internet
Author: User

Assuming that your ' re using Entity Framework 6, you already has a logging tool that can give you some insights into the SQ L your queries is generating and the time they take to run. The Database property of your DbContext object have a Log property that would give you that information. To turn on logging, you just has the to set the Log property to a method of that would write to a Log. This example sets-logging to-write Entity Framework messages to the Debug window:

Dim db as Salesorderentitiesdb = New salesorderentities () db. Database.Log = AddressOf Debug.WriteLine

In C #, you won ' t need the ADDRESSOF keyword. You do not have the to touch your code to make the 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.I Nfrastructure. 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's the output looks like (slightly edited):

Opened connection at 08-nov-16 12:15:51 pm-05:00select 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:sqldatareaderclosed Connect Ion at 08-nov-16 12:15:51 pm-05:00

When I used the first method on one of my tables.

Find Your Entity Framework Query is really Doing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.