There is a dbcontext context in EF, and if each access to the database produces a dbcontext that's a bad idea, it's a good practice to have a dbcontext instance once in a single request, and you can store dbcontext to the current request thread area.
The test uses the current request thread store code:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 6 namespaceRuntimedemo7 {8 /// <summary>9 ///to test the storage of thread valuesTen /// </summary> One class Program A { - Static voidMain (string[] args) - { theSystem.Runtime.Remoting.Messaging.CallContext.SetData ("Valuekey","value stored in the current request thread area");//storing values in the current request thread area stores the values in the current thread area to be recovered by the sleeping thread being recycled - - stringstr = System.Runtime.Remoting.Messaging.CallContext.GetData ("Valuekey") as string;//gets the value stored in the thread area of the current request - + Console.WriteLine (str); - + console.readline (); A at } - } -}
The above code runs the result:
One request to share an instance (value) to store (instance) values in the current request thread area