When does ef (Entity Framework) Submit a query to the database? -- the SQL event Probe

Source: Internet
Author: User

Currently, the projects at hand are made of LINQ, EF, and MVC, And I am confused by cainiao. Two days ago, I finally had some free time, and it took me some time to solve a long-standing question: When did EF submit a query to the database? Now let's share my little gains.

Why do I have the following questions:

Use the SQL database northwind and simple ASP. NET WebsiteProgramFor example, create a new default ASP. NET Website, add ADO. NET Entity Data Model to the project, and select orders and order_details tables in the Wizard. After completion, add a gridview on the default page default. aspx and type the followingCode.

 

Protected void page_load (Object sender, eventargs E)
{
Northwindmodel. northwindentities Ne = new northwindmodel. northwindentities ();
Gridview1.datasource = ne. orders;
Gridview1.databind ();
}

After running the program, the database connection, query, and binding are completed in three simple sentences. In this case, the three codes "When did EF submit a query to the database ?" This problem does not seem necessary to be pursued. However, in a specific system, because of the Code encapsulation in the system architecture, it is not that simple for new users like me. In addition, our powerful vs ide also adds to my doubts.

For example, close the program, place a breakpoint in front of the first code, run the program, and press F10 after the program is stopped. At this time, only 1st sentences of code are executed, and then move the mouse over the ne variable, in this case, the "tree menu" should appear (I don't know what the name is... Then I was surprised to find that all their data in the database can be seen in the two attributes orders and order_details of NE. I cannot help wondering? Does EF get data at this time, and it is still all rows in all tables! I don't think so. If so, no one will use EF.

SQL event Probe

With this question, I posted a post on csdn,"How to monitor or obtain the communication information between the program and the SQL database", And someone recommended SQL Server Profiler. In SQL2000, it is called an event probe, and it is under the query analyzer (such a conspicuous position, I never paid attention to it, AH ).

Open the time probe, create a new trail, and connect to the database instance (the database to which the northwind belongs ). Then, when our program accesses the database, the event probe will record the specific access information, such as the access time, the query statement executed, and the process ID.

Next, close the running website program, change it to the following code, and place a breakpoint in the first sentence.

 

  Northwindmodel. northwindentities ne  =    New  Northwindmodel. northwindentities ();
VaR result = From P In Ne. Orders Where P. employeeid = 1 Select P;
Gridview1.datasource = Result;
Gridview1.databind ();

Run the program. After the program is stopped, we keep running by pressing F10,

After executing the first code, we found that the program did not submit the query to the database, so we did not retrieve all the table data.

After the second code is executed, the program still does not submit the query to the database, which means that we only write one query statement, instead of submitting it to the database, then we can modify the query statement in the following statement.

After executing the third sentence of code, I guess I should have submitted it here. I didn't expect that I still didn't submit it here.

The last sentence of code is executed. Now the code is submitted.

In this case, EF submits queries to the database only when data needs to be retrieved and assigned to other objects.

Powerful

So: For another question, it is easy to understand the data of all tables in the ne variable after the first code is executed. It is the credit of, vs submitted the query to the database when we expanded the ne variable attribute value, instead of the program we wrote.

 

Because of a question, I have learned a lot about the event probe. From the perspective of the functions of the event probe, using it to optimize website programs will be a good tool.

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.