private static void UpdateClinics() { var strBuilder = new StringBuilder(); using (var ctx = new HealthDirContext()) using (var juneCtx = new DataJune.HdJune2011Context()) { var clinicsAdded = 0; var clinicsModified = 0; var juneClinics = juneCtx.Clinics.Where(c => !string.IsNullOrEmpty(c.AdmEmail)).ToList(); var currentClinics = ctx.Clinics.ToList(); foreach (var juneClinic in juneClinics) { try { var existingClinics = currentClinics.Where(c => c.Name.Equals(juneClinic.Name, StringComparison.OrdinalIgnoreCase)).ToList(); if (existingClinics.Any()) { foreach (var existingClinic in existingClinics) { if (string.IsNullOrWhiteSpace(existingClinic.AdmEmail) || existingClinic.AdmEmail.Equals("empty")) { ValidateClinic(existingClinic); existingClinic.AdmEmail = juneClinic.AdmEmail; AppendLinesToStrBuilder(existingClinic, strBuilder); Console.WriteLine($"\t\t{clinicsModified}. Clinics Modified"); clinicsModified++; } } } else { var mappedClinic = Mapper.Map <Clinics>(juneClinic); ValidateClinic(mappedClinic); ctx.Clinics.Add(mappedClinic); AppendLinesToStrBuilder(mappedClinic, strBuilder); Console.WriteLine($"{clinicsAdded}. Clinics Added"); clinicsAdded++; } } catch (Exception e) { Console.WriteLine(e); } } try { ctx.SaveChanges(); } catch (Exception ex) { throw ex; } Console.WriteLine($"Modified: {clinicsModified}, \t\t Added: {clinicsAdded} "); File.WriteAllText($"{Directory.GetCurrentDirectory()}\\file-june.txt", strBuilder.ToString()); Console.WriteLine("Success"); } }
private static void UpdateAreas() { using (var ctx = new HealthDirContext()) using (var juneCtx = new DataJune.HdJune2011Context()) { var currentClinicsWithourAreas = ctx.Clinics.Include("Areas").Where(c => !c.Areas.Any()).ToList(); var index = 0; foreach (var clinic in currentClinicsWithourAreas) { var areas = new List <DataJune.Areas>(); try { areas = juneCtx.Clinics.Include("Areas") .Single(c => c.Name.Equals(clinic.Name, StringComparison.OrdinalIgnoreCase)) .Areas.ToList(); } catch { // ignored } if (!areas.Any()) { continue; } foreach (var area in areas) { if (clinic.Areas.All(c => c.Name != area.Name)) { var currentArea = ctx.Areas.First(c => c.Name.Equals(area.Name, StringComparison.OrdinalIgnoreCase)); clinic.Areas.Add(currentArea); Console.WriteLine($"{++index}.Added area"); } } } try { ctx.SaveChanges(); Console.WriteLine($"\t\t\tSaved changes for {index} items!"); } catch (Exception ex) { Console.WriteLine(ex); throw ex; } Console.WriteLine("Success!"); } }
private static void UpdateCategories() { using (var ctx = new HealthDirContext()) using (var juneCtx = new DataJune.HdJune2011Context()) { var currentClinicsWithoutCategories = ctx.Clinics.Include("Categories").Where(c => !c.Categories.Any()) .ToList(); var index = 0; foreach (var currentClinic in currentClinicsWithoutCategories) { List <DataJune.Categories> categories; try { categories = juneCtx.Clinics.Include("Categories") .Single(c => c.Name.Equals(currentClinic.Name, StringComparison.OrdinalIgnoreCase)) .Categories.ToList(); } catch { continue; } if (index == 2000 || index == 5000 || index == 10000) { try { ctx.SaveChanges(); Console.WriteLine($"\t\t\tSaved changes for {index} items!"); } catch (Exception ex) { Console.WriteLine(ex); throw ex; } } if (!categories.Any()) { continue; } foreach (var category in categories) { if (currentClinic.Categories.All(c => c.Name != category.Name)) { var currentCategory = ctx.Categories.First(c => c.Name.Equals(category.Name, StringComparison.OrdinalIgnoreCase)); currentClinic.Categories.Add(currentCategory); Console.WriteLine($"{++index}.Added category"); } } } try { ctx.SaveChanges(); Console.WriteLine($"\t\t\tSaved changes for {index} items!"); } catch (Exception ex) { Console.WriteLine(ex); throw ex; } Console.WriteLine("Success!"); } }