private void ImportEntities <TSource, TTarget>(
            Func <DbContext, List <TSource> > getListFromSource,
            Action <TSource, TTarget> fillTargetWithSource,
            Func <TTarget, object> getTargetKey,
            Func <TSource, object> getSourceKey
            )
            where TSource : class
            where TTarget : EntityBase, new()
        {
            using (var flightAppsDb = new FlightAppsDbContext())
            {
                flightAppsDb.Configuration.LazyLoadingEnabled   = false;
                flightAppsDb.Configuration.ProxyCreationEnabled = false;

                var sourceEntitiesList = getListFromSource(flightAppsDb);
                Log(string.Format("\tSource: {0} items", sourceEntitiesList.Count));

                using (var infrastructureDb = new InfrastructureDbContext())
                {
                    infrastructureDb.Configuration.LazyLoadingEnabled   = false;
                    infrastructureDb.Configuration.ProxyCreationEnabled = false;

                    var localTargets = infrastructureDb.Set <TTarget>().ToDictionary(getTargetKey);
                    Log(string.Format("\tLocal: {0} items", localTargets.Count));

                    var updateCount = 0;
                    var insertCount = 0;
                    foreach (var sourceEntity in sourceEntitiesList)
                    {
                        var targetEntity = localTargets.GetValueOrDefault(getSourceKey(sourceEntity), null);

                        if (targetEntity == null)
                        {
                            targetEntity = new TTarget();
                            ((IXafEntityObject)targetEntity).OnCreated();
                            infrastructureDb.Set <TTarget>().Add(targetEntity);
                            insertCount++;
                        }
                        else
                        {
                            updateCount++;
                        }

                        fillTargetWithSource(sourceEntity, targetEntity);
                    }

                    Log(string.Format("\tUpdates/Insert: {0}/{1}", updateCount, insertCount));

                    var sw = Stopwatch.StartNew();
                    infrastructureDb.SaveChanges();
                    Log(string.Format("\tSave time: {0} ms", sw.ElapsedMilliseconds));
                    sw.Stop();
                }
            }
        }
        /// <summary>
        /// ////////////////////////////////////////////////////////////////////////////
        /// </summary>
        private void ImportFlightLegs()
        {
            using (var flightAppsDb = new FlightAppsDbContext())
            {
                flightAppsDb.Configuration.LazyLoadingEnabled   = false;
                flightAppsDb.Configuration.ProxyCreationEnabled = false;

                var start = StartDate.Date;
                var end   = EndDate.Date;

                var query =
                    from item in flightAppsDb.Set <VW_OperationsMaster>()
                    where item.Scheduled_DepartureDate_UTC.Value >= start && item.Scheduled_DepartureDate_UTC.Value < end
                    select item;

                var list =
                    query
                    .Include(item => item.FLT_FlightNumber)
                    .Include(item => item.FLT_FlightType)
                    .Include(item => item.GI_AircraftRegister)
                    .Include(item => item.FLT_Route_FlightLeg)
                    .Include(item => item.GI_Airport_ARRIVAL)
                    .Include(item => item.GI_Airport_DEPARTURE)
                    .Include(item => item.GI_AircraftType)
                    .ToList();

                var buckets =
                    (
                        from item in list
                        group item by
                        new DateTime(item.Scheduled_DepartureDate_UTC.Value.Year,
                                     item.Scheduled_DepartureDate_UTC.Value.Month, 1)
                        into dayBucket
                        orderby dayBucket.Key descending
                        select dayBucket
                    ).ToList();

                Log(string.Format("\tSource: {0} items", list.Count));
                Log(string.Format("\tBuckets: {0} items", buckets.Count()));


                foreach (var bucket in buckets)
                {
                    ProcessBucket(bucket);
                }
            }
        }