I am new to ASP.Net MVC and I have written a number of Controller classes that have this ivar:
private ApplicationDbContext db = new ApplicationDbContext();
I have also read that each request creates a new instance of the Controller.
I am concerned about database connection churn.
Should I be using a single global connection object instead? Or is there connection pooling happening behind the scenes and I shouldn't worry about it?
For Entity Framework ORM tool, it is not recommended to use DbContext
as a singleton instance , so, avoid to keep it on the global.asax. A good pratice is to use it with the pattern Unit of Work
. Read this article .
In this another article you can see sample pros and cons about how to keep an instance of DbContext.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.