title: The cast to value type 'System.Int32' failed because the materialized value is null. But it works on similar code
body: <p>In my controller I am searching the database for specific records in an association table.</p>
  
  <pre><code>public ActionResult MyAction(int picId, int myOtherId)
  {
      var itemToEdit =
          db.AssocationTable.Single(
              x =&gt; x.PropertyId == picId &amp;&amp; x.MyForeignKey == myOtherId);
  
       var myRecord = db.TestTableOne.Find(itemToEdit.PropertyId);
  
      var lstOfIntsINeed =
          db.AssocationTable.Where(
                  x =&gt; x.PropertyId == picId &amp;&amp; x.MyForeignKey != myOtherId)
              .Select(x =&gt; x.MyForeignKey.Value)
              .ToList();
  
      // the list above, if nothing is found, will give me an empty list where Count == 0
  }
  </code></pre>
  
  <p>Now I am doing the <strong>SAME EXACT</strong> code but for a different property and I am receiving:</p>
  
  <blockquote>
    <p>The cast to value type 'System.Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type. </p>
  </blockquote>
  
  <p>Here is my code for that:</p>
  
  <pre><code>public ActionResult MyOtherAction(int picId, int myOtherId)
  {
      var itemToEdit =
          db.AssociationTable.Single(
              x =&gt; x.PropertyId == picId &amp;&amp; x.MyOtherForeignKey == myOtherId);
  
      var myMainRecord = db.TestTableOne.Find(itemToEdit.PropertyId);
  
      var lstOfIntsINeed =
          db.AssociationTable.Where(
                  x =&gt; x.PropertyId == picId &amp;&amp; x.MyOtherForeignKey != myOtherId)
              .Select(x =&gt; x.MyOtherForeignKey.Value)
              .ToList();
  }
  </code></pre>
  
  <p>In both my database and my Domain Model Class for my Association Table both <code>MyForeignKey</code> and <code>MyOtherForeignKey</code> are nullable:</p>
  
  <pre><code>    [ForeignKey("TestTableTwo")]
      public int? MyForeignKey { get; set; }
  
      [ForeignKey("TestTableThree")]
      public int? MyOtherForeignKey { get; set; }
  </code></pre>
  
  <p>So, my question.. why in the first ActionResult is the lst receiving a count of 0 (which is what I expect), and the second ActionResult is receiving the exception error?</p>