private async Task UpdateFactions() { var factions = await Request_Get_Factions(new Id(1)); using (var DB = new FactionContext()) { CleanupFactionDB(factions, DB); foreach (var faction in factions.factions) { var Faction = DB.Factions.FirstOrDefault(F => faction.factionId == 0 ? F.Abbrev == faction.abbrev : F.FactionId == faction.factionId) ?? new Faction(); var IsNewFaction = string.IsNullOrEmpty(Faction.Abbrev); Faction.FactionId = faction.factionId; Faction.Name = faction.name; Faction.Origin = faction.origin; Faction.Abbrev = faction.abbrev; if (IsNewFaction) { DB.Factions.Add(Faction); } } if (DB.SaveChanges() > 0) { FactionHub?.Clients.All.SendAsync("Update", JsonConvert.SerializeObject(DB.Factions)).Wait(); } } }
public void AddFactionToDB(Faction aFaction) { using (var DB = new FactionContext()) { DB.Add(aFaction); DB.SaveChanges(); } FactionHub?.Clients.All.SendAsync("Update", JsonConvert.SerializeObject(aFaction)).Wait(); }
private static void CleanupFactionDB(FactionInfoList factions, FactionContext DB) { try { var dictFactions = factions.factions.ToDictionary(K => K.abbrev, K => K); DB.Factions .Where(F => !dictFactions.ContainsKey(F.Abbrev)) .ToList() .ForEach(F => DB.Factions.Remove(F)); DB.SaveChanges(); } catch (System.Exception Error) { System.Console.WriteLine(Error); } }