I am making an application in WPF, and I use Entity Framework v6.1.3 as ORM, database sql server 2012 as a database.
The query is simple. Make an insert of 8 thousand records approximately, it takes me more than 15 minutes. What really takes that amount of time is when you run SaveChanges.
My repository class is as follows.
public class RelevamientosRepository:ABMRepository
{
public void AddModels(List<Relevamiento> relevamientos)
{
using (EnsayosContext _context = new EnsayosContext())
{
_context.Configuration.AutoDetectChangesEnabled = false;
foreach (Relevamiento relevamiento in relevamientos)
{
relevamiento.TipoRelevamiento = null;
relevamiento.Parcela = null;
_context.Entry(relevamiento).State = (relevamiento.EsNuevo) ? EntityState.Added : EntityState.Modified;
}
_context.SaveChanges();
}
}
}
In turn in the context I have an override of the savechanges, to save date and time of insert or modification.
public override int SaveChanges()
{
foreach (var history in this.ChangeTracker.Entries()
.Where(e => e.Entity is IModificationHistory && (e.State== EntityState.Added || e.State== EntityState.Modified))
.Select(e => e.Entity as IModificationHistory))
{
history.DateModified = DateTime.Now;
if (history.DateCreated == DateTime.MinValue)
{
history.DateCreated = DateTime.Now;
}
}
int result = base.SaveChanges();
return result;
}
How to optimize data recording?