private static void TryAddOrUpdateFaction(MinorFaction faction) { DbContextOptionsBuilder <EliteDbContext> config = new DbContextOptionsBuilder <EliteDbContext>(); config.UseNpgsql(_connectionString); using (EliteDbContext context = new EliteDbContext(config.Options)) { long i = context.MinorFaction.Where(f => f.Name == faction.Name).Select(f => f.Id).FirstOrDefault(); long hsid = context.StarSystem.Where(ss => ss.EddbId == faction.HomeSystemId).Select(ss => ss.Id).FirstOrDefault(); faction.HomeSystemId = hsid; if (i == 0 && hsid != 0) { context.MinorFaction.Add(faction); context.SaveChanges(); Console.WriteLine($"Added '{faction.Name}'"); } else if (hsid != 0) { context.MinorFaction.Update(faction); context.SaveChanges(); Console.WriteLine($"Updated '{faction.Name}'"); } } }
private static void TryAddOrUpdateFactionPresence(MinorFactionPresence mfp, StarSystem ss) { DbContextOptionsBuilder <EliteDbContext> config = new DbContextOptionsBuilder <EliteDbContext>(); config.UseNpgsql(_connectionString); using (EliteDbContext context = new EliteDbContext(config.Options)) { mfp.StarSystemId = context.StarSystem.Where(s => s.EddbId == ss.EddbId).Select(s => s.Id).FirstOrDefault(); if (mfp.StarSystemId == 0) { return; } if (context.MinorFactionPresence.Where(m => m.StarSystemId == mfp.StarSystemId && m.MinorFactionId == mfp.MinorFactionId).Count() == 0) { context.MinorFactionPresence.Add(mfp); context.SaveChanges(); Console.WriteLine("Added faction presence for system #" + mfp.StarSystemId); } else { context.MinorFactionPresence.Update(mfp); context.SaveChanges(); } } }
private static bool TryAddOrUpdateBody(Body body) { DbContextOptionsBuilder <EliteDbContext> config = new DbContextOptionsBuilder <EliteDbContext>(); config.UseNpgsql(_connectionString); using (EliteDbContext context = new EliteDbContext(config.Options)) { try { StarSystem system = context.StarSystem.Where(ss => ss.EdsmId == body.StarSystemId).FirstOrDefault(); body.StarSystemId = system.Id; Body temp = context.Body.Where(b => b.EdsmId == body.EdsmId).FirstOrDefault(); if (system != null && temp != null) { body.Id = temp.Id; context.Body.Update(body); context.SaveChanges(); Console.WriteLine("Updated: " + body.Name); return(true); } else // for now, only add populated system bodies or bodies within 300 LY { if (system != null && (system.IsPopulated || system.DistanceTo(new StarSystem { X = 0, Y = 0, Z = 0 }) <= 250)) { context.Body.Add(body); context.SaveChanges(); Console.WriteLine(body.Name); return(true); } } return(false); } catch (DbUpdateException e) { //Debugger.Break(); return(false); } catch { //File.AppendAllText(@"E:\debug_bodies.txt", $"edsm,{body.Name},{body.Id}\n"); return(false); } } }
public DatabaseFixture() { var config = new DbContextOptionsBuilder <EliteDbContext>(); config.UseInMemoryDatabase("tempdb"); Context = new EliteDbContext(config.Options); Context.StarSystem.AddRange(TestSystems); Context.SaveChanges(); }
private static void TryAddOrUpdateAtmosComp(AtmosphereComposite ac) { DbContextOptionsBuilder <EliteDbContext> config = new DbContextOptionsBuilder <EliteDbContext>(); config.UseNpgsql(_connectionString); using (EliteDbContext context = new EliteDbContext(config.Options)) { if (context.AtmosphereComposite.Count(r => r.Component == ac.Component && r.BodyId == ac.BodyId) == 0) { context.AtmosphereComposite.Add(ac); context.SaveChanges(); } else { context.AtmosphereComposite.Update(ac); context.SaveChanges(); } } }
private static void TryAddOrUpdateRing(Ring ring) { DbContextOptionsBuilder <EliteDbContext> config = new DbContextOptionsBuilder <EliteDbContext>(); config.UseNpgsql(_connectionString); using (EliteDbContext context = new EliteDbContext(config.Options)) { if (context.Ring.Count(r => r.Name == ring.Name && r.BodyId == ring.BodyId) == 0) { context.Ring.Add(ring); context.SaveChanges(); } else { context.Ring.Update(ring); context.SaveChanges(); } } }
private static void TryAddOrUpdateMaterialShare(RawMaterialShare mat, string name) { DbContextOptionsBuilder <EliteDbContext> config = new DbContextOptionsBuilder <EliteDbContext>(); config.UseNpgsql(_connectionString); using (EliteDbContext context = new EliteDbContext(config.Options)) { mat.MaterialId = context.Material.Where(m => string.Equals(m.Name, name, StringComparison.OrdinalIgnoreCase)).Single().Id; if (context.RawMaterialShare.Count(r => r.MaterialId == mat.MaterialId && r.BodyId == mat.BodyId) == 0) { context.RawMaterialShare.Add(mat); context.SaveChanges(); } else { context.RawMaterialShare.Update(mat); context.SaveChanges(); } } }
private static void TryAddOrUpdateIngredient(Material material) { DbContextOptionsBuilder <EliteDbContext> config = new DbContextOptionsBuilder <EliteDbContext>(); config.UseNpgsql(_connectionString); using (EliteDbContext context = new EliteDbContext(config.Options)) { var temp = context.Material.Select(m => m.Name.Equals(material.Name)).Count(); if (temp == 0) { context.Material.Add(material); context.SaveChanges(); Console.WriteLine("Added material '" + material.Name + "'"); } else { material.Id = context.Material.Where(m => m.Name.Equals(material.Name)).Select(m => m.Id).First(); context.Material.Update(material); context.SaveChanges(); Console.WriteLine("Updated material '" + material.Name + "'"); } } }
private static void TryAddOrUpdateSystem(StarSystem system, bool shouldUpdate = true) { DbContextOptionsBuilder <EliteDbContext> config = new DbContextOptionsBuilder <EliteDbContext>(); config.UseNpgsql(_connectionString); using (EliteDbContext context = new EliteDbContext(config.Options)) { long i = context.StarSystem.Where(ss => ss.Name == system.Name).Select(ss => ss.Id).FirstOrDefault(); if (i == 0)// && system.IsPopulated) { context.StarSystem.Add(system); context.SaveChanges(); Console.WriteLine($"Added '{system.Name}'"); } else if (shouldUpdate) { system.Id = i; context.StarSystem.Update(system); context.SaveChanges(); Console.WriteLine($"Updated '{system.Name}'"); } } }
private static void SetSystemNameLower() { DbContextOptionsBuilder <EliteDbContext> config = new DbContextOptionsBuilder <EliteDbContext>(); config.UseNpgsql(_connectionString); using (EliteDbContext context = new EliteDbContext(config.Options)) { foreach (StarSystem ss in context.StarSystem) { ss.NameLower = ss.Name.ToLower(); context.Update(ss); } context.SaveChanges(); } }