title: SQL 2 LINQ query (called by databinding) completely freezing WPF application
body: <p>Earlier today I was hunting down a very weird bug... I finally traced it
  down to what seems to be causing the problem.</p>
  
  <p>The original report can be found here: <a href="https://stackoverflow.com/questions/262017/weird-behaviour-when-running-clickonce-deployed-version-of-wpf-application">original question</a></p>
  
  <p>The details have changed enough to warrant a new question.</p>
  
  <p>It would seem my application sometimes, NOT ALL OF THE TIME, freezes
  when it reaches the following LINQ query:</p>
  
  <pre><code>using (NetworkDatabaseContext db = new 
  
  NetworkDatabaseContext(UISession.ConnectionString))
  {
     Ballast ballast = db.Ballasts.FirstOrDefault(b =&gt; b.NetworkId == UISession.NetworkId &amp;&amp;
            b.ShortAddress == this.innerBallast.ShortAddress &amp;&amp; b.ControllerSerial == this.controllerSerial);
  </code></pre>
  
  <p>This is what it looks like:</p>
  
  <p><a href="https://i.stack.imgur.com/w8OYA.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/w8OYA.jpg" alt="Not Responding"></a></p>
  
  <p>Most of the time this works just fine... but every now and then it will lock up.
  This code is part of a BallastListItem class. Items of this class are bound to a ListBox on the Page:</p>
  
  <pre><code>&lt;ListView Name="lstBallasts" Margin="5" DockPanel.Dock="Top" MinHeight="100"&gt;&lt;!-- The MinHeight is used to get a good view in the designer --&gt;
          &lt;ListView.View&gt;
              &lt;GridView&gt;
                  &lt;GridViewColumn Header="Address" DisplayMemberBinding="{Binding InnerBallast.ShortAddress}" Width="70"/&gt;
                  &lt;GridViewColumn Header="Name" Width="300"&gt;
                      &lt;GridViewColumn.CellTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;TextBox Name="txtBallastDisplayName" Text="{Binding DisplayName}" Width="270" MaxWidth="270" MaxLength="100"/&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/GridViewColumn.CellTemplate&gt;
                  &lt;/GridViewColumn&gt;
                  &lt;GridViewColumn Header="Type" DisplayMemberBinding="{Binding DeviceType}" Width="150"/&gt;
                  &lt;GridViewColumn Header="Status" DisplayMemberBinding="{Binding InnerBallast.StandardVersion}" Width="150"/&gt;
              &lt;/GridView&gt;
          &lt;/ListView.View&gt;
      &lt;/ListView&gt; 
  </code></pre>
  
  <p>The code is part of the DisplayName property getter:</p>
  
  <pre><code>public string DisplayName
      {
          get
          {
              using (NetworkDatabaseContext db = new NetworkDatabaseContext(UISession.ConnectionString))
              {
                  Ballast ballast = db.Ballasts.FirstOrDefault(b =&gt; b.NetworkId == UISession.NetworkId &amp;&amp; 
                     b.ShortAddress == this.innerBallast.ShortAddress &amp;&amp; b.ControllerSerial == this.controllerSerial);
  </code></pre>
  
  <p>So this code is called when databinding occurs, should have realized that before.
  Still I have no idea why this would sometimes cause problems...</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>In both cases (when the application freezes and when it does not)
  the state of the connection is "Closed" just before the query code, as I can see from:</p>
  
  <pre><code>db.Connection.State.ToString()
  </code></pre>
  
  <p><strong>UPDATE 2</strong>
  I forgot to mention I moved that code out of the getter,
  so it is only fetched once. Must have been sleeping while implementing
  this the first time. The code works fine now, although on XP... every now
  and then, but much less often the problem still occurs.</p>
  
  <p><strong>* UPDATE 3 *</strong>
  Just to be clear, I'm using an SQL Compact database with very little data</p>