static void Main(string[] args) { DiseasesContext ctx = new DiseasesContext(); // clear DB ctx.Diseases.RemoveRange(ctx.Diseases); ctx.Names.RemoveRange(ctx.Names); ctx.Causes.RemoveRange(ctx.Causes); ctx.SaveChanges(); // dir must be in bin/Debug/netcoreapp3.0 string dirPath = "files"; Regex re = new Regex($@"{dirPath}\\([a-zA-Z ]+)\.txt"); foreach (string filePath in Directory.GetFiles(dirPath, "*.txt")) { string category = re.Match(filePath).Groups[1].Value; Console.WriteLine(category); Console.WriteLine(string.Concat(Enumerable.Repeat("=", category.Length + 4))); List <Disease> diseases = new List <Disease>(); foreach (string line in File.ReadAllText(filePath).Split('\n')) { Disease disease = new Disease(); Name name = new Name(disease, line.Trim()); ctx.Diseases.Add(disease); ctx.Names.Add(name); Console.WriteLine($"Added {uline(disease.StringName)}"); diseases.Add(disease); } Console.WriteLine(); Random rand = new Random(); for (int i = 0; i < diseases.Count; i += 1) { for (int j = 0; j < rand.Next(1, 5); j += 1) { int di = -1; do { di = rand.Next(diseases.Count); } while (i == di); Cause cause = new Cause(diseases[i], diseases[di]); try { ctx.Causes.Add(cause); ctx.SaveChanges(); Console.WriteLine($"{uline(diseases[i].StringName)} is comorbid with {uline(diseases[di].StringName)}"); } catch { ctx.Causes.Remove(cause); j -= 1; } } } } }
private void clearContext(DiseasesContext context) { context.Diseases.RemoveRange(context.Diseases); context.Names.RemoveRange(context.Names); context.Causes.RemoveRange(context.Causes); context.SaveChanges(); }
public void WhenNewDiseaseInsertedThenUpdateSuccessful() { using (DiseasesContext context = new DiseasesContext()) { clearContext(context); Disease disease = new Disease(); context.Diseases.Add(disease); context.SaveChanges(); Assert.Equal(1, context.Diseases.Count()); } }
public void WhenNewCauseIsInsertedThenItMustBeUnique() { using (DiseasesContext context = new DiseasesContext()) { clearContext(context); Disease disease1 = new Disease(), disease2 = new Disease(); Cause cause1 = new Cause(disease1, disease2), cause2 = new Cause(disease1, disease2); context.Diseases.Add(disease1); context.Diseases.Add(disease2); context.Causes.Add(cause1); context.SaveChanges(); try { context.Causes.Add(cause2); context.SaveChanges(); } catch (DbUpdateException e) { Assert.Equal(1, context.Causes.Count()); } } }