Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to query Entity Framework before calling DbContext.SaveChanges?

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.

like image 929
JKasper11 Avatar asked Sep 19 '13 21:09

JKasper11


People also ask

How do I get ID before SaveChanges?

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.

When should you call context SaveChanges?

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.

What does the DbContext SaveChanges () method return?

Returns. The number of state entries written to the underlying database. This can include state entries for entities and/or relationships.


1 Answers

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

like image 76
Alireza Avatar answered Oct 05 '22 07:10

Alireza