private void importType(JToken model, Data.Contexts.DomainModelMySqlContext context) { var typeName = (string)model["ModelTypeName"]; var fileName = (string)model["Name"]; fileName = _inputDirectory + "extracted-files" + Path.DirectorySeparatorChar + fileName + ".json"; var oneAtTheTime = _globalOneAtTheTime; // check if we should import one at the time (this is used for debugging) if (model["ImportOneAtTheTime"] != null) { oneAtTheTime = (bool)model["ImportOneAtTheTime"]; } Console.Write("seeding data: " + typeName + " .... "); // get types var myType = Type.GetType(typeName); var myListType = typeof(List <>).MakeGenericType(myType); var items = (IEnumerable <object>)JsonConvert.DeserializeObject(File.ReadAllText(fileName), myListType); var objItems = items.ToArray <object>(); if (!oneAtTheTime) { context.AddRange(objItems); try { context.SaveChanges(); } catch (Exception ex) { Console.Write(ex.ToString()); } } else { var count = objItems.Count(); var ind = 0; Console.Write(" " + count.ToString() + " items ... "); // lets add one by one foreach (var item in objItems) { ind++; if (ind % 10 == 0) { Console.Write("."); } try { context.Add(item); context.SaveChanges(); } catch (Exception ex) { Console.WriteLine("EXCEPTION ADDING OBJECT:"); Console.WriteLine(item); throw ex.InnerException; } } } Console.Write("done!" + Environment.NewLine); }
private void importTypeWithReader(JToken model, Data.Contexts.DomainModelMySqlContext context) { var typeName = (string)model["ModelTypeName"]; var fileName = (string)model["Name"]; fileName = _inputDirectory + "extracted-files" + Path.DirectorySeparatorChar + fileName + ".json"; var oneAtTheTime = _globalOneAtTheTime; // check if we should import one at the time (this is used for debugging) if (model["ImportOneAtTheTime"] != null) { oneAtTheTime = (bool)model["ImportOneAtTheTime"]; } Console.WriteLine("seeding data: " + typeName + " .... "); // get types var myType = Type.GetType(typeName); var myListType = typeof(List <>).MakeGenericType(myType); var logTimes = 0; // used for debugging context.ChangeTracker.AutoDetectChangesEnabled = false; var commitCount = 100000; var objItems = new HashSet <object>(); using (var stream = File.OpenRead(fileName)) using (StreamReader streamReader = new StreamReader(stream)) using (JsonTextReader reader = new JsonTextReader(streamReader)) { reader.SupportMultipleContent = true; var serializer = new JsonSerializer(); while (reader.Read()) { if (reader.TokenType == JsonToken.StartObject) { var myObject = serializer.Deserialize(reader, myType); objItems.Add(myObject); // save to every x-objects if (objItems.Count() == commitCount) { Console.WriteLine("inserting " + (++logTimes * commitCount).ToString("#,##0")); context.AddRange(objItems); try { context.SaveChanges(); } catch (Exception ex) { Console.Write(ex.ToString()); } objItems = new HashSet <object>(); } } } if (objItems.Any()) { Console.WriteLine("inserting: " + objItems.Count().ToString("#,##0")); context.AddRange(objItems); context.SaveChanges(); } } Console.Write("done!" + Environment.NewLine); }