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.