private static void DeleteUserData(IKTContext db) { foreach (MemberTrait MembreTrait in db.MemberTraits.AsTracking()) { db.Entry(MembreTrait).State = EntityState.Deleted; } foreach (MemberPsychic MembrePsychique in db.MemberPsychics.AsTracking()) { db.Entry(MembrePsychique).State = EntityState.Deleted; } foreach (MemberWeapon MembreArme in db.MemberWeapons.AsTracking()) { db.Entry(MembreArme).State = EntityState.Deleted; } foreach (MemberPower MembrePouvoir in db.MemberPowers.AsTracking()) { db.Entry(MembrePouvoir).State = EntityState.Deleted; } foreach (MemberWarGearOption MembreRemplacement in db.MemberWarGearOptions.AsTracking()) { db.Entry(MembreRemplacement).State = EntityState.Deleted; } foreach (Member Membre in db.Members.AsTracking()) { db.Entry(Membre).State = EntityState.Deleted; } foreach (Team equipe in db.Teams.AsTracking()) { db.Entry(equipe).State = EntityState.Deleted; } db.SaveChanges(); }
public static void SetSerializedData(IKTContext db, string data, bool deleteFirst = true, Dictionary <string, Dictionary <string, string> > replacements = default) { SaveModel saveModel = JsonConvert.DeserializeObject <SaveModel>(DecompressString(data)); if (deleteFirst) { DeleteUserData(db); } if (replacements != null && replacements.Count != 0) { saveModel = ReplaceData(saveModel, replacements); } var existing_options = db.Set <WarGearOption>() .Where(wgo => saveModel.Teams .SelectMany(t => t.Members) .SelectMany(m => m.MemberWarGearOptions) .Select(m => m.WarGearOptionId) .Contains(wgo.Id) ) .Select(wgo => wgo.Id) .ToList(); // import everything List <Team> equipes = saveModel.Teams; foreach (Team equipe in equipes) { db.Entry(equipe).State = EntityState.Added; foreach (Member membre in equipe.Members) { db.Entry(membre).State = EntityState.Added; foreach (MemberTrait MembreTrait in membre.MemberTraits) { db.Entry(MembreTrait).State = EntityState.Added; } foreach (MemberPsychic MembrePsychique in membre.MemberPsychics) { db.Entry(MembrePsychique).State = EntityState.Added; } foreach (MemberWeapon MembreArme in membre.MemberWeapons) { db.Entry(MembreArme).State = EntityState.Added; } foreach (MemberPower MembrePouvoir in membre.MemberPowers) { db.Entry(MembrePouvoir).State = EntityState.Added; } foreach (MemberWarGearOption MembreRemplacement in membre.MemberWarGearOptions) { // if the id still exists add it, otherwise skip if (existing_options.Contains(MembreRemplacement.WarGearOptionId)) { db.Entry(MembreRemplacement).State = EntityState.Added; } } } } db.SaveChanges(); }
public static void SetSerializedData( IKTContext db, string data, bool deleteFirst = true, Dictionary <string, Dictionary <string, string> > replacements = default, Action <float?, string> callback = null ) { callback?.Invoke(0, "Reading backup"); SaveModel saveModel = JsonConvert.DeserializeObject <SaveModel>(DecompressString(data)); int size = saveModel.Size; if (deleteFirst) { DeleteUserData(db); } callback?.Invoke(0, "Cleaning up old data"); if (replacements != null && replacements.Count != 0) { saveModel = ReplaceData(saveModel, replacements); } var existing_options = db.Set <WarGearOption>() .Where(wgo => saveModel.Teams .SelectMany(t => t.Members) .SelectMany(m => m.MemberWarGearOptions) .Select(m => m.WarGearOptionId) .Contains(wgo.Id) ) .Select(wgo => wgo.Id) .ToList(); // import everything int progress = 0; List <Team> equipes = saveModel.Teams; foreach (Team equipe in equipes) { db.Entry(equipe).State = EntityState.Added; foreach (Member membre in equipe.Members) { float percent = (float)progress / size; callback?.Invoke(percent, $"Adding {membre.Name} from {equipe.Name}"); progress += 1; db.Entry(membre).State = EntityState.Added; foreach (MemberTrait MembreTrait in membre.MemberTraits) { db.Entry(MembreTrait).State = EntityState.Added; } foreach (MemberPsychic MembrePsychique in membre.MemberPsychics) { db.Entry(MembrePsychique).State = EntityState.Added; } foreach (MemberWeapon MembreArme in membre.MemberWeapons) { db.Entry(MembreArme).State = EntityState.Added; } foreach (MemberPower MembrePouvoir in membre.MemberPowers) { db.Entry(MembrePouvoir).State = EntityState.Added; } foreach (MemberWarGearOption MembreRemplacement in membre.MemberWarGearOptions) { // if the id still exists add it, otherwise skip if (existing_options.Contains(MembreRemplacement.WarGearOptionId)) { db.Entry(MembreRemplacement).State = EntityState.Added; } } } } db.SaveChanges(); }