Home >Backend Development >C++ >Should I Use One DbContext Per HTTP Request in My Application?
In the ASP.NET CORE application, the creation of a new DBContext instance for each HTTP request is widely recommended. The reason is as follows:
Avoid data cache problems and performance improvement
DBContext can cache the data. If multiple requests access the same data at the same time, the data may be expired. DBContext using a single mode will limit the data cache to the scope of a single request, reduce the database calling, thereby improving performance.
The advantage of each request a DBContext
Single business affairs: Multiple operations are executed in a DBContext to facilitate transaction processing and ensure data integrity.
_context.SaveChanges()
The disadvantages of the transient DBContext Each object must call to save changes, increase complexity, and violate the principle of single responsibilities.
Entity scope limit:The entity cannot leave the scope of the class that loaded them, and cannot be used in other classes.
context.SaveChanges()
Other optional solutions The decorative device can be applied to the command processing program to ensure the correct transaction management and submit it only when successful.
The above is the detailed content of Should I Use One DbContext Per HTTP Request in My Application?. For more information, please follow other related articles on the PHP Chinese website!