title: Is it possible to query Entity Framework before calling DbContext.SaveChanges?
body: <p>In this simple example, I have two entities: Event and Address. I have a console application running every night to import event data from an XML source and add it to my database.</p>
  
  <p>As I loop through the XML event nodes (inside of the Entity Framework context), I check to see if there is an address record with the given values already in the database. If not, it adds a new record.</p>
  
  <pre><code>using (DemoContext context = new DemoContext())
  {
      foreach (XmlNode eventNode in eventsXml.SelectNodes("/Events/Event"))
      {
          Event newEvent = new Event();
  
          newEvent.Title = **get from XML**
  
          Address address = context.Addresses.Where(a =&gt;
              a.Title.Equals(title, StringComparison.OrdinalIgnoreCase) &amp;&amp;
              a.Address1.Equals(address1, StringComparison.OrdinalIgnoreCase) &amp;&amp;
              a.Address2.Equals(address2, StringComparison.OrdinalIgnoreCase) &amp;&amp;
              a.City.Equals(city, StringComparison.OrdinalIgnoreCase) &amp;&amp;
              a.State.Equals(state, StringComparison.OrdinalIgnoreCase) &amp;&amp;
              a.ZipCode.Equals(zipCode, StringComparison.OrdinalIgnoreCase)
          ).FirstOrDefault();
  
          if (address != null)
              newEvent.Location = address;
          else
          {
              newEvent.Location.Title = title;
              newEvent.Location.Address1 = address1;
              newEvent.Location.Address2 = address2;
              newEvent.Location.City = city;
              newEvent.Location.State = state;
              newEvent.Location.ZipCode = zipCode;
          }
  
          context.Events.Add(newEvent);
      }
  
      context.SaveChanges();
  }
  </code></pre>
  
  <p>I know it is slower performance to call context.SaveChanges() after every event, so I would like to do this all at the end (or do it in batches but that's not relevant to this problem). However, when I query against context.Addresses, it doesn't seem to be aware of any new addresses until after I call context.SaveChanges() so I get duplicate records.</p>
  
  <p>For my purposes it may be okay to save after each record rather than at the end, but I would like to know if there is a good, simple alternative.</p>