Esempio n. 1
0
        public IEnumerable <T> GetAll(string[] includes = null)
        {
            //HANDLE INCLUDES FOR ASSOCIATED OBJECTS IF APPLICABLE
            if (includes != null && includes.Count() > 0)
            {
                var query = dataContext.Set <T>().Include(includes.First());
                foreach (var include in includes.Skip(1))
                {
                    query = query.Include(include);
                }
                return(query.AsQueryable());
            }

            return(dataContext.Set <T>().AsQueryable());
        }
        private static CarDealerDbContext AddToContext <T>(CarDealerDbContext context,
                                                           T entity, int count, int commitCount, bool recreateContext, int totalElements, DateTime startTime) where T : class
        {
            Task.Run(async() =>
            {
                await context.Set <T>().AddRangeAsync(entity);
            }).Wait();

            if (count % commitCount == 0)
            {
                Task.Run(async() =>
                {
                    await context.SaveChangesAsync();
                }).Wait();
                if (recreateContext)
                {
                    var percent = ((double)count / totalElements) * 100;
                    context.Dispose();
                    context = new CarDealerDbContext();
                    if (count == 500)
                    {
                        Console.WriteLine("Data is being seeded... Please wait!");
                    }
                    if (count % 3000 == 0)
                    {
                        var timeSpent = DateTime.Now - startTime;
                        var percentForCalculatingRemainingTime = (double)count / totalElements;
                        var totalTime     = (1.0 / percentForCalculatingRemainingTime) * timeSpent;
                        var remainingTime = totalTime - timeSpent;

                        if (remainingTime.Hours > 0)
                        {
                            Console.WriteLine("Completed: {0:f2}%. Remaining time: {1:%h} hours {1:%m} minutes and {1:%s} seconds.", percent, remainingTime);
                        }
                        if (remainingTime.Hours == 0 && remainingTime.Minutes > 1 && remainingTime.Seconds > 1)
                        {
                            Console.WriteLine("Completed: {0:f2}%. Remaining time: {1:%m} minutes and {1:%s} seconds.", percent, remainingTime);
                        }

                        switch (remainingTime.Minutes)
                        {
                        case 1 when(remainingTime.Hours == 0 && remainingTime.Seconds > 1):
                            Console.WriteLine("Completed: {0:f2}%. Remaining time: {1:%m} minute and {1:%s} seconds.",
                                              percent, remainingTime);
                            break;

                        case 0 when(remainingTime.Hours == 0 && remainingTime.Seconds > 1):
                            Console.WriteLine("Completed: {0:f2}%. Remaining time: {1:%s} seconds.", percent,
                                              remainingTime);
                            break;
                        }

                        if (remainingTime.Seconds == 1 && remainingTime.Hours == 0 && remainingTime.Minutes == 0)
                        {
                            Console.WriteLine("Completed: {0:f2}%. Remaining time: {1:%s} second.", percent, remainingTime);
                        }
                    }
                    context.ChangeTracker.AutoDetectChangesEnabled = false;
                }
            }

            return(context);
        }