, then the context of our entire project will have many instances of the instance, we have encountered several times, when we are in the programming of the time when we meet a lot of times, we have to think about can solve the problem more.
(2) Here I would like to say is the EF context how to manage it? Very simple, is to ensure that the thread is unique, so here we are going to modify the Baserepository class.
(3) What is the duty of baserepository warehousing here? His duty is to help us achieve the public method of all sub-warehousing (additions and deletions), his duties do not include how to manage the context of the instance, so we can not put this control contexts instance of the thread within the unique code in this position, this is the responsibility of each of our classes must be unique, The point of object-oriented is that the responsibilities of a class must be singular.
(4) Take a look at our modified baserepository (warehousing), here I only list a small part, because the following are not changed
1NamespaceLyzj. Userlimitmvc.dal3{5///<summary>7///Base class for implementing operations on the database (additional deletions and checks)9///</smmary>11///<typeparam name= "T" >Defining generics, constraining them to be a class</typeparam>13PublicClass baserepository<t>where T:Class15{1617//Create the context for the EF framework19//An instance of the EF context guarantees that the thread is unique within21st//Private Datamodelcontainer db = new Datamodelcontainer ();25Private DbContext db =Efcontextfactory.getcurrentdbcontext ();2627//Add functionality to the database, add references to implement the EF framework2829PublicT addentity (t entity)31{3233//EF4.0 Add an Entity35//Db. Createobjectset<t> (). AddObject (entity);37//The writing of EF5.0. Entry<t> (entity). State = entitystate.added; 43 db. SaveChanges (); Return Entity; }49 Span style= "line-height:1.5!important;" >}51}
(5) So how do we control the instance of the context and ask that it be unique within the thread? At this time we can not put in the baserepository (warehousing) to set up, when we think of the encapsulation, we will control the context of the instance and require that it is a thread within the unique code encapsulated into a common class. How do we do that? Take a look at the following procedure
(6) This time we see the above code we can not directly implement the new (//private datamodelcontainer db = new Datamodelcontainer ();), then how do we get this instance? The point is that the place where the instances are obtained must be public, and also be able to help us manage the uniqueness of the threads, when we can imagine that we can implement this example through the factory, then we create a efcontextfactory factory here, where there are The Getcurrentdbcontext () method returns the instance (private DbContext db = Efcontextfactory.getcurrentdbcontext ();).
Then we need to create the class above.
6.EFContextFactory
(1) At this time we are in the LYZJ.UserLimitMVC.DAL class library to build a efcontextfactory class, in this class contains Getcurrentdbcontext methods, the following I explain the implementation of the Code, we implement this method of code is:
NamespaceLyzj.Userlimitmvc.DAL
{
Public Class Efcontextfactory
{
Help us return to the database context within the current thread, create a context if there is no context within the current thread, and guarantee
On-line question instance inside the thread is unique
Public Static DbContext Getcurrentdbcontext()
{
CallContext: is the only data slot (a piece of memory space) that is unique within the thread
Pass DbContext in to get the information of the instance, where it is cast.
DbContextDbContext= CallContext.GetData("DbContext") As DbContext;
If (DbContext== Null) The thread does not have this context in the data slot
{
DbContext = new< Span class= "PLN" > datamodelcontainer (); //if there is no context, create an EF context
//we're creating one, put it in the data slot
callcontext. Setdata ( "DbContext" , DbContext }
return }
}
} /span>
Processing of the EF DbContext context