private void RecurseCreation(object item, EntityInfoCollection <EntityInfo> entities)
        {
            Random rand       = new Random(DateTime.Now.Millisecond * DateTime.Now.Minute);
            string entityName = entities.GetNameForType(item.GetType());

            if (entities.HasEntity(entityName) && entities[entityName].References.Count > 0)
            {
                object primarykey = entities[entityName].Fields.KeyField.PropertyInfo.GetValue(item, null);
                foreach (var refentity in entities[entityName].References)
                {
                    if (this._stopGenerator)
                    {
                        break;
                    }
                    var             genType   = typeof(List <>).MakeGenericType(refentity.ReferenceEntityType);
                    var             items     = (System.Collections.IList)Activator.CreateInstance(genType);
                    Model.modelbase reference = (Model.modelbase)Activator.CreateInstance(refentity.ReferenceEntityType);
                    int             iCount    = rand.Next(this._config.GenerateChildrenBaseAmount);
                    for (int i = 0; i < iCount; i++)
                    {
                        if (this._stopGenerator)
                        {
                            break;
                        }
                        var newelement = reference.CreateRandomObject(primarykey);
                        AddMessage(newelement.ToString());
                        items.Add(newelement);
                        //this._DataStore.Insert(newelement);
                        RecurseCreation(newelement, entities);
                    }
                    if (refentity.IsArray)
                    {
                        var arr = Array.CreateInstance(refentity.ReferenceEntityType, items.Count);
                        items.CopyTo(arr, 0);
                        refentity.PropertyInfo.SetValue(item, arr, null);
                    }
                    else if (refentity.IsList)
                    {
                        refentity.PropertyInfo.SetValue(item, items, null);
                    }
                    else
                    {
                        refentity.PropertyInfo.SetValue(item, items[0], null);
                    }
                }
            }
        }
 private void ThreadedCreation()
 {
     try
     {
         if (this._DataStore != null)
         {
             Boolean useTransations = this._config.UseTransactionsForInserts;
             var     entities       = this._DataStore.GetEntities();
             int     baseAmount     = this._config.GenerateDataBaseAmount;
             var     list           = new List <EntityInfo>();
             int     iEntity        = 0;
             foreach (var entity in entities)
             {
                 Type type = entity.EntityType;
                 // Filtering only topmost objects
                 var parent = (from el in entities
                               where el.References.Any(p => p.ReferenceEntityType.Equals(type))
                               select el).FirstOrDefault();
                 if (parent == null)
                 {
                     list.Add(entity);
                 }
             }
             if (list.Count > 0)
             {
                 double maxValue = list.Count * 2.5 * baseAmount;
                 double speed    = 0;
                 foreach (var entity in list)
                 {
                     if (this._stopGenerator)
                     {
                         break;
                     }
                     Random          rand           = new Random(DateTime.Now.Millisecond * DateTime.Now.Minute);
                     int             iCount         = baseAmount + rand.Next(2 * baseAmount);
                     Model.modelbase item           = (Model.modelbase)Activator.CreateInstance(entity.EntityType);
                     Boolean         bHasReferences = (entity.References.Count > 0);
                     var             start          = DateTime.Now;
                     for (int i = 0; i < iCount; i++)
                     {
                         if (this._stopGenerator)
                         {
                             break;
                         }
                         var newelement = item.CreateRandomObject();
                         AddMessage(newelement.ToString());
                         RecurseCreation(newelement, entities);
                         this._DataStore.Insert(newelement, bHasReferences, useTransations);
                         Progress(iEntity++ / maxValue);
                     }
                     speed = DateTime.Now.Subtract(start).TotalMilliseconds / iCount;
                     System.Diagnostics.Debug.WriteLine(String.Format("{1} average speed: {0}ms/item", speed, entity.EntityName));
                     AddMessage(String.Format("{1} average speed: {0}ms/item", speed, entity.EntityName));
                 }
             }
         }
     }
     catch (Exception ex)
     {
         OpenNETCF.ORM.MainDemo.Logger.LogException(System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
     }
     finally
     {
         RefreshUI();
     }
 }