EF instantiate Context

Source: Internet
Author: User

I have learned that it takes a long time for EF to instantiate an entity. I just tested it and used the following code:

 

Code

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. diagnostics;

Namespace consoleapplication1
{
Class Program
{
Static void main (string [] ARGs)
{
Stopwatch Sw = new stopwatch ();
Sw. Reset ();
Sw. Start ();
TestEntities entity = new testEntities ();
List <User> uselist = entity. User. Where (p => p. UserPwd = "111"). ToList ();
Entity. Dispose ();
Sw. Stop ();

Console. WriteLine (sw. ElapsedMilliseconds. ToString () + "ms" + "count:" + uselist. Count. ToString ());
Sw. Reset ();
Sw. Start ();

TestEntities entity2 = new testEntities ();
List <User> uselist2 = entity2.User. Where (p => p. UserPwd = "111"). ToList ();
Entity2.Dispose ();
Sw. Stop ();

Console. WriteLine (sw. ElapsedMilliseconds. ToString () + "ms" + "count:" + uselist2.Count. ToString ());

Console. Read ();

}
}
}

 

Execution result:

388 Ms

11 ms

It can be seen that EF is slow when it is opened for the first time, and it will soon become

I also tested it using a web project. I took an entity instance on a page and jumped to another page to instantiate the entity again. The result was the same as that of the console program. The first time it took time and the second time it was very fast, in fact, EF is based on ado.net, so its new connection is also obtained from the connection pool, so the second time is much faster.

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.