Reputation: 17253
I currently have something like this
public class FooController : ApiController
{
[HttpPost]
public string CreateSomething(string action)
{
DBModelMgr m = new DBModelMgr();
.......
return ...;
}
}
Now DBModelMgr is a little resource intensive. Is there a way for me to have it instantiated once ? If I make it a member variable of the class it will still get created everytime CreateSomething method is called ? Correct ? Please correct me if I am wrong. Also I was thinking about making it a static variable but that would result in cross thread issues. Any suggestions on how I can handle this situation ?
Upvotes: 1
Views: 37
Reputation: 6427
Now DBModelMgr is a little resource intensive
What makes you think this?
Infact DBContexts are light-weight objects and should indeed be instantiated when required and disposed straight after.
When working with Web applications, use a context instance per request.
(https://learn.microsoft.com/en-us/ef/ef6/fundamentals/working-with-dbcontext)
Upvotes: 1