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.
You can retreive an ID before calling . SaveChanges() by using the Hi/Lo alhorithm. The id will be assigned to the object once it is added to dbcontext.
If you need to enter all rows in one transaction, call it after all of AddToClassName class. If rows can be entered independently, save changes after every row.
Returns. The number of state entries written to the underlying database. This can include state entries for entities and/or relationships.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With