public async Task<string> GenerateMySQLReport() { var list = new List<JObject>(); using (WeaponSystemContext msSqlServerContext = new WeaponSystemContext()) { await msSqlServerContext .Weapons .Select(w => new { Id = w.Id, Name = w.Name, Manufacturer = w.Manufacturer.Name }) .ForEachAsync(w => list.Add(CreateJObject(w.Id, w.Name, w.Manufacturer)) ); } using (WeaponReportsDbContext MySqlServerContext = new WeaponReportsDbContext()) { MySqlActions.AddReports(list, MySqlServerContext); } return MySQLSuccessMessage; }
public static void AddReports(IList<JObject> reports, WeaponReportsDbContext db) { for (int i = 0; i < reports.Count; i++) { if (i == reports.Count - 1) { AddReport(reports[i], db); } else { AddReport(reports[i], db, true); } } }
public static void AddReport(JObject report, WeaponReportsDbContext db, bool isAddingMany = false) { var weaponReport = new WeaponReports(); var weaponId = report.GetValue("weapon-id"); var weaponName = report.GetValue("weapon-name"); var manufacturer = report.GetValue("manufacturer"); weaponReport.WeaponID = weaponId.Value<int>(); weaponReport.WeaponName = weaponName.Value<string>(); weaponReport.Manufacturer = manufacturer.Value<string>(); db.WeaponReports.Add(weaponReport); if (!isAddingMany) { db.SaveChanges(); } }