public static async Task <bool> AddSectionsToDatabaseAsync(DescDBContext dBContext, ICollection <SectionInfo> sections) { foreach (var section in sections) { var exists = await CheckForSectionAsync(dBContext, section).ConfigureAwait(true); if (exists) { sections.Remove(section); } } try { using (DescDBContext dbContext = new DescDBContext()) { dbContext.AddRange(sections); await dbContext.SaveChangesAsync().ConfigureAwait(true); return(true); } } catch (Exception) { throw new Exception(string.Format("Error adding sections to database")); } }
private static DescDBContext AddMeasurementsToContext(DescDBContext context, IEnumerable <Measurement> measurements, int count, int commitCount, bool recreateContext) { context.AddRange(measurements); //Detach all child records which already have been added otherwise I get the following error // Cannot insert explicit value for identity column in table ... when IDENTITY_INSERT is set to OFF. //Measurements can only get descriptions and subintervals which have already been loaded foreach (var measurement in measurements) { //try detaching only the subinterval // context.Entry(measurement.LithologicSubinterval).State = Microsoft.EntityFrameworkCore.EntityState.Detached; //context.Entry(measurement.LithologicDescription).State = Microsoft.EntityFrameworkCore.EntityState.Detached; context.Entry(measurement.LithologicDescription).State = Microsoft.EntityFrameworkCore.EntityState.Unchanged; foreach (LithologicSubinterval subInterval in measurement.LithologicDescription.LithologicSubintervals) { if (measurement.LithologicSubinterval.ID != 0) { context.Entry(subInterval).State = Microsoft.EntityFrameworkCore.EntityState.Unchanged; } } if (measurement.LithologicSubinterval.ID != 0) { context.Entry(measurement.LithologicSubinterval).State = Microsoft.EntityFrameworkCore.EntityState.Unchanged; } //DescriptionColumnValuePairs are tracked and accessed via the measurement's LithologicSubinterval property foreach (DescriptionColumnValuePair entry in measurement.LithologicDescription.DescriptionColumnValues) { context.Entry(entry).State = Microsoft.EntityFrameworkCore.EntityState.Unchanged; } if (measurement.SectionInfo.ID != 0) { context.Entry(measurement.SectionInfo).State = Microsoft.EntityFrameworkCore.EntityState.Unchanged; } } if (count % commitCount == 0) { context.SaveChanges(); if (recreateContext) { context.Dispose(); context = new DescDBContext(); } } return(context); }
private static DescDBContext AddToContext <TEntity>(DescDBContext context, IEnumerable <TEntity> entities, int count, int commitCount, bool recreateContext) where TEntity : class { context.AddRange(entities); if (count % commitCount == 0) { context.SaveChanges(); if (recreateContext) { context.Dispose(); context = new DescDBContext(); } } return(context); }
private static DescDBContext AddLithologiesToContext(DescDBContext context, IEnumerable <LithologicDescription> descriptions, int count, int commitCount, bool recreateContext) { context.AddRange(descriptions); //Detach all child records which already have been added otherwise I get the following error // Cannot insert explicit value for identity column in table 'Sections' when IDENTITY_INSERT is set to OFF. foreach (var description in descriptions) { if (description.SectionInfo.ID != 0) { context.Entry(description.SectionInfo).State = Microsoft.EntityFrameworkCore.EntityState.Unchanged; } foreach (LithologicSubinterval subInterval in description.LithologicSubintervals) { //Detach subintervals already added (and have non-zero primary keys) if (subInterval.ID != 0) { context.Entry(subInterval).State = Microsoft.EntityFrameworkCore.EntityState.Unchanged; } //Detach subinterval sectioninfo already added (and have non-zero primary keys) if (subInterval.SectionInfo.ID != 0) { context.Entry(subInterval.SectionInfo).State = Microsoft.EntityFrameworkCore.EntityState.Unchanged; } } } if (count % commitCount == 0) { context.SaveChanges(); if (recreateContext) { context.Dispose(); context = new DescDBContext(); } } return(context); }