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.
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.
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 =>
a.Title.Equals(title, StringComparison.OrdinalIgnoreCase) &&
a.Address1.Equals(address1, StringComparison.OrdinalIgnoreCase) &&
a.Address2.Equals(address2, StringComparison.OrdinalIgnoreCase) &&
a.City.Equals(city, StringComparison.OrdinalIgnoreCase) &&
a.State.Equals(state, StringComparison.OrdinalIgnoreCase) &&
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();
}
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.
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.
When you query in a way that the database is touched, then the newly added entities in the context are not included in the result.
In EF 4.1 you can get them via DbSet<T>.Local
See :
Why do Entity Framework queries not return unsaved entities
And
Entity Framework: Re-finding objects recently added to context