Last post May 10, 2017 11:50 AM by kaushalparik27
May 10, 2017 10:13 AM|Ravindranath M|LINK
What is the best way to use the edmx object creation in the multiple pages in the entire solution.
Case 1 :
if (DBEntityObj == null)
DBEntityObj = new ApplicationProj();
Case 2: using (ApplicationProj DBEntityObj = new ApplicationProj())
But we are using so many times in the project. which needs to use?
May 10, 2017 10:54 AM|kaushalparik27|LINK
As per my opinion, Case 2 should be used. The reason for the "using" statement is to ensure that the object is disposed as soon as it goes out of scope, and it doesn't require explicit code to ensure that this happens. And specially in case of EDMX, using()
statement can avoid max connection pooling reached error that can occur if connections are not disposed.
Here are some related reads if you want to refer:
May 10, 2017 11:33 AM|Ravindranath M|LINK
but using case 1, will it creates multiple instances as we are checking whether the object is null reference only we are creating new instance of it.
May 10, 2017 11:50 AM|kaushalparik27|LINK
It will not create multiple instances, but in case 1 you will require to declare the context object to be accessible at page/controller level or project level (like in base class of all controllers or page, if you want to use only single object thru out).
Also, you may need to manually dispose the object when you are done using it. Using() blocks will automatically dispose the object once control is out of Using() block/scope.