Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF SaveChanges optimisation?

Why DbContext ctx disposing after each SaveChanges() perform more quickly?

First sample:

var ctx = new DomainContext(); 
foreach (var item in deals)
        {
            DealsOfReutersAddition newDealAddition =
                        new DealsOfReutersAddition
                        {
                            DealsOfReutersId = item.DealsOfReutersId,
                            DealDate = DateTime.Parse(item.DateConfirmed ?? "01 01 01", new CultureInfo("en-US")),
                        };
            ctx.DealsOfReutersAdditions.Add(newDealAddition);
            ctx.SaveChanges();
        }

ctx.Dispose();

Second sample:

foreach (var item in deals)
        {
            DealsOfReutersAddition newDealAddition =
                        new DealsOfReutersAddition
                        {
                            DealsOfReutersId = item.DealsOfReutersId,
                            DealDate = DateTime.Parse(item.DateConfirmed ?? "01 01 01", new CultureInfo("en-US")),
                        };
            var ctx = new DomainContext(); 
            ctx.DealsOfReutersAdditions.Add(newDealAddition);
            ctx.SaveChanges();
            ctx.Dispose();
        }

On 1000 rows second sample estimated at 8 seconds aganist of 140 seconds in first. Any way to clean ctx instead recreating?

like image 797
FreeVice Avatar asked Jun 15 '26 15:06

FreeVice


1 Answers

Along the lines of what @JensKloster was saying in the comments, you definitely don't want to call SaveChanges every single time in the loop. But I would actually resist the temptation of putting it outside of the loop. As the number of objects being tracked by the context gets higher and higher, more memory will be used up, and it will make saving incrementally slow.

My solution for you, then, is to have a running counter in the loop to determine when you would do the save:

int numberOfRecords = 0;
using(var ctx = new DomainContext())
{
  foreach (var item in deals)
          {
              DealsOfReutersAddition newDealAddition =
                          new DealsOfReutersAddition
                          {
                              DealsOfReutersId = item.DealsOfReutersId,
                              DealDate = DateTime.Parse(item.DateConfirmed ?? "01 01 01", new CultureInfo("en-US")),
                          };

              ctx.DealsOfReutersAdditions.Add(newDealAddition);

              numberOfRecords++;
              if(numberOfRecords % 500 == 0) //Saves after every 500 rows.
              {
                ctx.SaveChanges();
              }
           }
}
like image 179
Corey Adler Avatar answered Jun 17 '26 05:06

Corey Adler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!