John Shedletsky
John Shedletsky

Reputation: 7168

Managing Database Connections in ASP.Net MVC

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?

Upvotes: 1

Views: 931

Answers (1)

Felipe Oriani
Felipe Oriani

Reputation: 38598

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.

Upvotes: 1

Related Questions