Home >Backend Development >C++ >Should I Use One DbContext Per Web Request in My Application?
Design concept
To create an independent DBContext instance for each web request for each web application. Compared with the use of a single DBContext instance for the entire application, this method has many advantages. advantages
Acting Domain: Each web request is running in its own domain, so as to achieve a clear transaction boundary. This simplifies the error treatment, and ensures data integrity by limiting the impact of failure operation in the current request.
isolation:The database's concurrency access volume is very high, and isolation is critical to prevent data conflict. The business logic is complicated, and clearly separates between data access and application logic.
The limitations of the instance of the transient DBContext
Entity scope: The entity loaded by a DBContext instance cannot be used in the context of another instance. This will complicate the code and cause performance problems.
Context ownership:
Display DBCONTEXT instances becomes more complicated, and it needs to be explicitly treated or automatically disposal at the request boundary.context.SaveChanges()
The above is the detailed content of Should I Use One DbContext Per Web Request in My Application?. For more information, please follow other related articles on the PHP Chinese website!