1

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.