title: Entity Framework throws exception when using Find-method
body: <p>I have a simple web application which allows an user to upload 2 .csv-files containing certain data. To persist the data I use the <code>Entity Framework</code> in two different <code>Import-methods</code>.</p>
  
  <p>First Import-method</p>
  
  <pre><code>    public void ImportOne(string path)
      {
          StreamReader sr = new StreamReader(path);
  
          using (var db = new ContextEv("RndContext"))
          {
              db.Database.ExecuteSqlCommand("DELETE FROM TableA");
              db.Database.ExecuteSqlCommand("DELETE FROM TableB");
  
              while (!sr.EndOfStream)
              {
                  string[] data = sr.ReadLine().Split(';');
                  string houseId = data[0];
  
                      House house = new House()
                      {
                          HouseId = houseId,
                      };
  
                      House dummy = db.Houses.Find(houseId);
  
                      if (!dummy.HouseId.Equals(house.HouseId))
                      {
                          db.Houses.Add(house);
                      }
                  }
              }
          }
      }
  </code></pre>
  
  <p>This line fails: <code>House dummy = db.Houses.Find(houseId);</code> with the following exception: </p>
  
  <blockquote>
    <p>The type of one of the primary key values did not match the type
    defined in the entity. See inner exception for
    details.\r\nParametername: keyValues</p>
  </blockquote>
  
  <p>ErrorContext of InnerException: </p>
  
  <blockquote>
    <p>keyword 'AS', line 1, column 22</p>
  </blockquote>
  
  <p>ErrorDescription of InnerException: </p>
  
  <blockquote>
    <p>The query syntax is not valid.</p>
  </blockquote>
  
  <p>Alright, I checked if really the type is the problem here. However I haven't found anything wrong. </p>
  
  <p>The "funny" thing about it is, that I use the same Find-method in another Import-method and it works without any exception!</p>
  
  <pre><code>using (var db = new ContextEv("RndContext"))
              {
                  db.Database.ExecuteSqlCommand("DELETE FROM TableC");
                  db.Database.ExecuteSqlCommand("DELETE FROM TableD");
  
              StreamReader sr = new StreamReader(path);
  
              while (!sr.EndOfStream)
              {
                  string[] data = sr.ReadLine().Split(';');
                  string houseId = data[5];
  
                      House house = db.Houses.Find(houseId);
  
                      ...
                      ...
                      db.SaveChanges();
                  }
              }
  </code></pre>
  
  <p>I wasn't sure which code is really needed for you to answer my question but I'd be very happy to post more if someone asks for a particular code.</p>
  
  <p><strong>UPDATE 1 ANSWER TO user89861</strong></p>
  
  <blockquote>
    <p>'db.Houses.ToList().Find(h => h.HouseId == houseId)' threw an
    exception of type 'System.NullReferenceException'</p>
    
    <p>"   bei
    System.Data.Entity.Internal.Linq.InternalQuery<code>1.GetEnumerator()\r\n<br>
    bei System.Data.Entity.Internal.Linq.InternalSet</code>1.GetEnumerator()\r\n
    bei
    System.Data.Entity.Infrastructure.DbQuery<code>1.System.Collections.Generic.IEnumerable&lt;TResult&gt;.GetEnumerator()\r\n
    bei System.Collections.Generic.List</code>1..ctor(IEnumerable<code>1
    collection)\r\n   bei
    System.Linq.Enumerable.ToList[TSource](IEnumerable</code>1 source)"</p>
  </blockquote>