private void SeedData() { try { // To make the seeding WAY faster we can "cheat" our way to it by using only changed data in the json. // The Data-Fetcher-Service is now outputting a json file with the correct format to get all changed data. // Seeding the data.json only if the data_web.json is not existent. // My Service is acutally now outputting a correct diff of the new and old Json files. If we delete the json File after seeding, everything will be fine and way faster than before. #if BETA string SeedingFile = "D:/ToH/Data/data_web_dev.json"; #else string SeedingFile = "D:/ToH/Data/data_web.json"; #endif if (!File.Exists(SeedingFile)) { return; } /* * if (File.Exists("D:/ToH/Data/data_web.json")) * { * // Get differences with the current file and the backuped file. * SeedingFile = "D:/ToH/Data/data_web.json"; * } */ using (var context = new ToH_Database_WebContext( _serviceProvider.GetRequiredService < DbContextOptions <ToH_Database_WebContext> >())) { // TODO: Deserialize ToH-Json File here. string FilePath = SeedingFile; if (File.Exists(FilePath)) { JsonSerializer serializer = new JsonSerializer(); serializer.Converters.Add(new BooleanJsonConverter()); // Get all Meta-Data of all models available. var types = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(s => s.GetTypes()) .Where(p => typeof(I_CfgModel).IsAssignableFrom(p) && p.IsClass && !p.IsAbstract); List <MetaData> metaModels = new List <MetaData>(); foreach (Type t in types) { metaModels.Add(new MetaData(t, context)); } for (int i = 0; i < metaModels.Count; i++) { using (FileStream s = File.Open(FilePath, FileMode.Open)) using (StreamReader sr = new StreamReader(s)) using (JsonTextReader reader = new JsonTextReader(sr)) { MetaData md = metaModels[i]; if (md.DBModel == null || md.DeserializingMethod == null || md.AddObject == null || md.UpdateObject == null) { continue; } var tbl = new DefaultJsonNameTable(); tbl.Add(md.JsonModel); reader.PropertyNameTable = tbl; bool UsedToHaveData = false; // Seems like formatted json code is actually slower to read as unformatted json code. while (reader.Read()) { if (reader.TokenType != JsonToken.StartObject) { continue; } if (i == 41) { int aasd = 0; } if (!reader.Path.Contains(md.JsonModel.Replace("data.", "") + ".")) { if (UsedToHaveData) { break; } continue; } UsedToHaveData = true; // Insert new Object or update existing ones. I_CfgModel c = (I_CfgModel)md.DeserializingMethod.Invoke(serializer, new object[] { reader }); if (c == null) { continue; } var a = context.Find(md.Type, new object[] { c.getID() }); if (a != null) { context.Entry(a).CurrentValues.SetValues(c); } else { md.AddObject.Invoke(md.DBModel, new object[] { c }); } } } } } context.SaveChanges(); } File.Delete(SeedingFile); } catch (Exception ex) { // TODO: Log ex. } }