Static field of DbContext in Global.asax versus instance field of DbContext in controller class?

后端 未结 2 1786
一向
一向 2020-12-10 08:26

Frankly I am a newbie in both C# and Asp.net MVC. I also do not know how the asp.net web application actually works on IIS and ASP.NET framework behind the scene.

I

2条回答
  •  隐瞒了意图╮
    2020-12-10 08:53

    You should go with the second option. ie declare and use it in the controller. If you put the DBContext as a static field in global.asax, you are basically making it a single instance for the entire application.

    On the other hand, with the second option you have a DBcontext for each request. It would be better if you can use a dependency injection to get the DBContext in each action method.

提交回复
热议问题