MeuEntities
is a class generated by Entity Framework, inheriting from Dbcontext, right?
Life cycle of the connection
Using the Entity Framework, you do not control the life cycle of the connection but rather the life cycle of the context. At specific times the context will require a connection with the bank to search or persist entities.
Physical connection to the database is managed by ADO Net, that maintains a pool of connections to avoid creating a new connection every time the consumer needs one, so it offers better performance since creating connection to the database has high cost.
Keep the life of the context short and let the management of connections with the Entity Framework and ADO.Net that it uses underneath.
And this you are already doing since you create the context just to persist and already get rid of it.
Relationship between the class being static and the connection life cycle.
There is no relationship. The scope of the context or fact that you discard it (either by invoking the method Disposis that is to instantiate it with the using) is that will affect the life cycle of the connection. The fact that the class consuming the context is static has no relation.
Now, if you declare the context in a static variable and never get rid of it, then you can negatively affect the connection lifecycle.
Finally
Remove the method call from your code Disposis for the using serves precisely to ensure this call.
Based on experience with another
Framework
I think it’s redundantdb.Dispose()
sinceusing
would automatically do this job.db.Dispose()
how muchusing
close the connection.– rubStackOverflow