Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you look at the MSDN here: <a href="http://msdn.microsoft.com/library/vstudio/bb896325" rel="nofollow">http://msdn.microsoft.com/library/vstudio/bb896325</a> </p> <blockquote> <p>The Entity Framework opens connections only when required, for example to execute a query or to call SaveChanges, and then closes the connection when the operation is complete. </p> <p>When a query method is called, the connection is opened, and it remains open until the ObjectResult has been completely consumed or disposed.</p> </blockquote> <pre><code>public void UpdateCategory(Models.Category catData) { if (catData == null) return; using (var cntx = new DataContext()) { //IN THE LINE BELOW A CONNECTION IS DEFINITELY OPENED BUT IS IT //IMMEDIATELY CLOSE? =&gt; YES! var cat = cntx.Set&lt;Category&gt;() .FirstOrDefault(c =&gt; c.CategoryId == catData.CategoryId); if (cat == null) return; if (!cat.LastChanged.Matches(catData.LastChanged)) throw new ConcurrencyException(cat.GetType().ToString()); cat.CategoryName = catData.CategoryName; cntx.DbContext.Entry&lt;Category&gt;(cat).State = System.Data.EntityState.Modified; //AFTER THE NEXT LINE DO I HAVE 2 CONNECTIONS OPENED? =&gt; NO cntx.SaveChanges(); catData.LastChanged = cat.LastChanged; } } </code></pre> <blockquote> <p>Are there scenerios where the same context creates\opens 2 connections, leaving one open?</p> </blockquote> <p>Yes, when using transaction with a DbContext it will open a second connection have a look here: <a href="http://www.digitallycreated.net/Blog/48/entity-framework-transactionscope-and-msdtc" rel="nofollow">http://www.digitallycreated.net/Blog/48/entity-framework-transactionscope-and-msdtc</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload