title: Selecting from context in Entity Framework
body: <p>I am reading data from a Comma-separated Value (CSV) document and saving its contents to a database. Each row in the CSV file contains the following columns:</p>
  
  <ul>
  <li>Customer ID</li>
  <li>Latitude</li>
  <li>Longitude</li>
  </ul>
  
  <p>Our domain model is as follows:</p>
  
  <ul>
  <li><code>Customer</code> has many <code>Locations</code></li>
  <li><code>Location</code>
  <ul>
  <li>Latitude</li>
  <li>Longitude</li>
  </ul></li>
  </ul>
  
  <p>For each row, I am reading the <code>Customer ID</code> and look it up in the database:</p>
  
  <pre><code>var customer = (from c in Context.Customer
                  where c.ID == id
                  select c).SingleOrDefault();
  </code></pre>
  
  <p>If it does not exist, I create one:</p>
  
  <pre><code>if (customer == null)
  {
      customer = new customers();
      customer.ID = id;
  }
  </code></pre>
  
  <p>I do not save it at this point. Next, I create a new <code>Location</code>, and set its customer. Note that this customer may not actually be in the database yet.</p>
  
  <p>Now consider the following: if a customer is not yet in the database, I create one. But I do not save it yet. Now the next row belongs to the same customer that I just created, it is not yet in the database. It will therefore create <strong>another</strong> customer.</p>
  
  <p>When the time comes to save the graph to database, it will try to save two distinct customers with the same ID and fail.</p>
  
  <p>How can I get Entity Framework to also select from what is in its context, even though it may not be persisted yet?</p>