Does DbContext holds an opened connection during its life-cycle?










-1














The question is pretty clear. Does DbContext hold an open connection during its life-cycle? What about EF core?










share|improve this question



















  • 2




    No and no, unless you force it by starting a transaction and keeping it open. Otherwise it only opens a connection when loading or saving and closes it once it's done. You can verify that easily using SQL Server profiler or Extended events though
    – Panagiotis Kanavos
    Nov 13 '18 at 9:37






  • 1




    Have you heard of connection-pooling? It applies also to DbContext and the connection-string specifies if it is used. See: ConnectionString (further down at pooling)
    – Rango
    Nov 13 '18 at 9:37







  • 1




    Why the question though? It sounds like you encountered a different problem and think connection management is at fault? Or think that pooled connections are somehow a problem? They aren't
    – Panagiotis Kanavos
    Nov 13 '18 at 9:37











  • @PanagiotisKanavos That's and answer. Post it to get accepted. Thank you.
    – javad amiry
    Nov 13 '18 at 9:38















-1














The question is pretty clear. Does DbContext hold an open connection during its life-cycle? What about EF core?










share|improve this question



















  • 2




    No and no, unless you force it by starting a transaction and keeping it open. Otherwise it only opens a connection when loading or saving and closes it once it's done. You can verify that easily using SQL Server profiler or Extended events though
    – Panagiotis Kanavos
    Nov 13 '18 at 9:37






  • 1




    Have you heard of connection-pooling? It applies also to DbContext and the connection-string specifies if it is used. See: ConnectionString (further down at pooling)
    – Rango
    Nov 13 '18 at 9:37







  • 1




    Why the question though? It sounds like you encountered a different problem and think connection management is at fault? Or think that pooled connections are somehow a problem? They aren't
    – Panagiotis Kanavos
    Nov 13 '18 at 9:37











  • @PanagiotisKanavos That's and answer. Post it to get accepted. Thank you.
    – javad amiry
    Nov 13 '18 at 9:38













-1












-1








-1







The question is pretty clear. Does DbContext hold an open connection during its life-cycle? What about EF core?










share|improve this question















The question is pretty clear. Does DbContext hold an open connection during its life-cycle? What about EF core?







c# entity-framework lifecycle dbconnection






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 16:38









mxmissile

7,28023867




7,28023867










asked Nov 13 '18 at 9:34









javad amiryjavad amiry

15.2k84698




15.2k84698







  • 2




    No and no, unless you force it by starting a transaction and keeping it open. Otherwise it only opens a connection when loading or saving and closes it once it's done. You can verify that easily using SQL Server profiler or Extended events though
    – Panagiotis Kanavos
    Nov 13 '18 at 9:37






  • 1




    Have you heard of connection-pooling? It applies also to DbContext and the connection-string specifies if it is used. See: ConnectionString (further down at pooling)
    – Rango
    Nov 13 '18 at 9:37







  • 1




    Why the question though? It sounds like you encountered a different problem and think connection management is at fault? Or think that pooled connections are somehow a problem? They aren't
    – Panagiotis Kanavos
    Nov 13 '18 at 9:37











  • @PanagiotisKanavos That's and answer. Post it to get accepted. Thank you.
    – javad amiry
    Nov 13 '18 at 9:38












  • 2




    No and no, unless you force it by starting a transaction and keeping it open. Otherwise it only opens a connection when loading or saving and closes it once it's done. You can verify that easily using SQL Server profiler or Extended events though
    – Panagiotis Kanavos
    Nov 13 '18 at 9:37






  • 1




    Have you heard of connection-pooling? It applies also to DbContext and the connection-string specifies if it is used. See: ConnectionString (further down at pooling)
    – Rango
    Nov 13 '18 at 9:37







  • 1




    Why the question though? It sounds like you encountered a different problem and think connection management is at fault? Or think that pooled connections are somehow a problem? They aren't
    – Panagiotis Kanavos
    Nov 13 '18 at 9:37











  • @PanagiotisKanavos That's and answer. Post it to get accepted. Thank you.
    – javad amiry
    Nov 13 '18 at 9:38







2




2




No and no, unless you force it by starting a transaction and keeping it open. Otherwise it only opens a connection when loading or saving and closes it once it's done. You can verify that easily using SQL Server profiler or Extended events though
– Panagiotis Kanavos
Nov 13 '18 at 9:37




No and no, unless you force it by starting a transaction and keeping it open. Otherwise it only opens a connection when loading or saving and closes it once it's done. You can verify that easily using SQL Server profiler or Extended events though
– Panagiotis Kanavos
Nov 13 '18 at 9:37




1




1




Have you heard of connection-pooling? It applies also to DbContext and the connection-string specifies if it is used. See: ConnectionString (further down at pooling)
– Rango
Nov 13 '18 at 9:37





Have you heard of connection-pooling? It applies also to DbContext and the connection-string specifies if it is used. See: ConnectionString (further down at pooling)
– Rango
Nov 13 '18 at 9:37





1




1




Why the question though? It sounds like you encountered a different problem and think connection management is at fault? Or think that pooled connections are somehow a problem? They aren't
– Panagiotis Kanavos
Nov 13 '18 at 9:37





Why the question though? It sounds like you encountered a different problem and think connection management is at fault? Or think that pooled connections are somehow a problem? They aren't
– Panagiotis Kanavos
Nov 13 '18 at 9:37













@PanagiotisKanavos That's and answer. Post it to get accepted. Thank you.
– javad amiry
Nov 13 '18 at 9:38




@PanagiotisKanavos That's and answer. Post it to get accepted. Thank you.
– javad amiry
Nov 13 '18 at 9:38












1 Answer
1






active

oldest

votes


















2














As noted by others, no, it doesn't, unless you manually open the connection and pass it to DbContext constructor.



A concrete detailed answer can be found here https://stackoverflow.com/a/45330219/191148.



And the comment of @ajcvickers in https://github.com/aspnet/EntityFrameworkCore/issues/7810 clears it:




If EF creates the DbConnection object, then EF will ensure it is
disposed when the DbContext is disposed. On the other hand, if some
other code creates the DbConnection object and passes it to EF, then
it is the responsibility of the other code to also dispose the
connection appropriately.



Similar rules apply to opening and closing the connection. If EF opens
the connection, then EF will close the connection when it is done with
it. If your code opens the connection, then your code should close the
connection.







share|improve this answer




















  • Thanks a lot. I also found this link: brentozar.com/archive/2015/07/… which is commented by Julie Lerman. She is describing what exactly happens to underlying connection.
    – javad amiry
    Nov 13 '18 at 17:44










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277880%2fdoes-dbcontext-holds-an-opened-connection-during-its-life-cycle%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














As noted by others, no, it doesn't, unless you manually open the connection and pass it to DbContext constructor.



A concrete detailed answer can be found here https://stackoverflow.com/a/45330219/191148.



And the comment of @ajcvickers in https://github.com/aspnet/EntityFrameworkCore/issues/7810 clears it:




If EF creates the DbConnection object, then EF will ensure it is
disposed when the DbContext is disposed. On the other hand, if some
other code creates the DbConnection object and passes it to EF, then
it is the responsibility of the other code to also dispose the
connection appropriately.



Similar rules apply to opening and closing the connection. If EF opens
the connection, then EF will close the connection when it is done with
it. If your code opens the connection, then your code should close the
connection.







share|improve this answer




















  • Thanks a lot. I also found this link: brentozar.com/archive/2015/07/… which is commented by Julie Lerman. She is describing what exactly happens to underlying connection.
    – javad amiry
    Nov 13 '18 at 17:44















2














As noted by others, no, it doesn't, unless you manually open the connection and pass it to DbContext constructor.



A concrete detailed answer can be found here https://stackoverflow.com/a/45330219/191148.



And the comment of @ajcvickers in https://github.com/aspnet/EntityFrameworkCore/issues/7810 clears it:




If EF creates the DbConnection object, then EF will ensure it is
disposed when the DbContext is disposed. On the other hand, if some
other code creates the DbConnection object and passes it to EF, then
it is the responsibility of the other code to also dispose the
connection appropriately.



Similar rules apply to opening and closing the connection. If EF opens
the connection, then EF will close the connection when it is done with
it. If your code opens the connection, then your code should close the
connection.







share|improve this answer




















  • Thanks a lot. I also found this link: brentozar.com/archive/2015/07/… which is commented by Julie Lerman. She is describing what exactly happens to underlying connection.
    – javad amiry
    Nov 13 '18 at 17:44













2












2








2






As noted by others, no, it doesn't, unless you manually open the connection and pass it to DbContext constructor.



A concrete detailed answer can be found here https://stackoverflow.com/a/45330219/191148.



And the comment of @ajcvickers in https://github.com/aspnet/EntityFrameworkCore/issues/7810 clears it:




If EF creates the DbConnection object, then EF will ensure it is
disposed when the DbContext is disposed. On the other hand, if some
other code creates the DbConnection object and passes it to EF, then
it is the responsibility of the other code to also dispose the
connection appropriately.



Similar rules apply to opening and closing the connection. If EF opens
the connection, then EF will close the connection when it is done with
it. If your code opens the connection, then your code should close the
connection.







share|improve this answer












As noted by others, no, it doesn't, unless you manually open the connection and pass it to DbContext constructor.



A concrete detailed answer can be found here https://stackoverflow.com/a/45330219/191148.



And the comment of @ajcvickers in https://github.com/aspnet/EntityFrameworkCore/issues/7810 clears it:




If EF creates the DbConnection object, then EF will ensure it is
disposed when the DbContext is disposed. On the other hand, if some
other code creates the DbConnection object and passes it to EF, then
it is the responsibility of the other code to also dispose the
connection appropriately.



Similar rules apply to opening and closing the connection. If EF opens
the connection, then EF will close the connection when it is done with
it. If your code opens the connection, then your code should close the
connection.








share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 16:36









Mohsen AfshinMohsen Afshin

9,95085175




9,95085175











  • Thanks a lot. I also found this link: brentozar.com/archive/2015/07/… which is commented by Julie Lerman. She is describing what exactly happens to underlying connection.
    – javad amiry
    Nov 13 '18 at 17:44
















  • Thanks a lot. I also found this link: brentozar.com/archive/2015/07/… which is commented by Julie Lerman. She is describing what exactly happens to underlying connection.
    – javad amiry
    Nov 13 '18 at 17:44















Thanks a lot. I also found this link: brentozar.com/archive/2015/07/… which is commented by Julie Lerman. She is describing what exactly happens to underlying connection.
– javad amiry
Nov 13 '18 at 17:44




Thanks a lot. I also found this link: brentozar.com/archive/2015/07/… which is commented by Julie Lerman. She is describing what exactly happens to underlying connection.
– javad amiry
Nov 13 '18 at 17:44

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277880%2fdoes-dbcontext-holds-an-opened-connection-during-its-life-cycle%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

Evgeni Malkin