I am new to Visual Studio. I am developing a MVC web project and I'm using static DbContext
. Because it is not a simple web page when a user sign in, he will use the program for a long time and I want it to be fast. What are the disadvantages of using static DbContext
?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- MVC-Routing,Why i can not ignore defaults,The matc
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
Since
DbContext
is not thread safe, if your application have async action then it's possible to have multiple thread using your DbContext, which can lead to an exception.On another hand, creating a new
DbContext
instance doesn't mean open a new connection to DB. Net Framework should use one of connections already open inConnection Pool
.If you only use one
DbContext
instance and lock it for thread safety, so you only have one connection to DB. If your website have hundreds of request per second then all of them have to queue to use the only connection. In that case, DbContext object became the perfomance bottleneck of your system.And there are tons of problem with data caching in EF when you working with static
DbContext
instance.So, it's better to create a new instance of DbContext for each request - let the framework manage the connection for us and don't worry it should fast.