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();
                }
            }
        }
        public override void Load()
        {
            System.Console.WriteLine("[{0}][配置源更新]", DateTimeOffset.Now.ToLocalTime());

            using var dbContext = new InfrastructureDbContext(_source.DbOptions);
            dbContext.Database.EnsureCreated();

            var options = dbContext.ChannelOptions.AsNoTracking().ToList();

            options.ForEach(option =>
            {
                var data       = option.JsonOptions.ToKeyValuePairs();
                var keyBuilder = new StringBuilder();
                foreach (var dataKv in data)
                {
                    keyBuilder.Clear();
                    var key = keyBuilder.Append(option.PostType).Append(':').Append(dataKv.Key).ToString();

                    // Data.Add(key, dataKv.Value);
                    Data[key] = dataKv.Value;
                }
            });
        }
Example #3
0
 public InfrastructureContextSeed(InfrastructureDbContext dbContext, IWebHostEnvironment env)
 {
     _dbContext = dbContext;
     _env       = env;
 }
        private void ProcessBucket(IGrouping <DateTime, VW_OperationsMaster> monthBucket)
        {
            Log(string.Format("\t--Bucket: {0} items", monthBucket.Key));
            using (var infrastructureDb = new InfrastructureDbContext())
            {
                infrastructureDb.Configuration.LazyLoadingEnabled   = false;
                infrastructureDb.Configuration.ProxyCreationEnabled = false;

                var existingFlightLegsOfMonthBucketQuery =
                    from item in infrastructureDb.Set <FlightLeg>()
                    where
                    item.ScheduledDepartureDateTime.DateTimeUtc.Value.Year == monthBucket.Key.Year &&
                    item.ScheduledDepartureDateTime.DateTimeUtc.Value.Month == monthBucket.Key.Month
                    select item;

                var existingFlightLegsOfMonthBucketGrouped = from fl in existingFlightLegsOfMonthBucketQuery.ToList()
                                                             group fl by string.Format("{0},{1},{2},{3}", fl.FlightNumberId, fl.AircraftRegisterId, fl.RepetitionReasonCode, fl.ScheduledDepartureDateTime.DateTimeUtc) into flGroup
                                                             select flGroup;

                var existingFlightLegsOfMonthBucket =
                    existingFlightLegsOfMonthBucketGrouped
                    .Select(i => i.First())
                    .ToDictionary(
                        fl =>
                        string.Format("{0},{1},{2},{3}", fl.FlightNumberId, fl.AircraftRegisterId,
                                      fl.RepetitionReasonCode, fl.ScheduledDepartureDateTime.DateTimeUtc));

                Log(string.Format("\tLocal: {0} items", existingFlightLegsOfMonthBucket.Count));

                long updateCount = 0;
                long insertCount = 0;

                foreach (var sourceEntity in monthBucket)
                {
                    var flightNumber =
                        flightNumbers.GetValueOrDefault(sourceEntity.FLT_FlightNumber.FlightNumber_AbvNumber, null);
                    var flightNumberId = flightNumber != null ? (Guid?)flightNumber.Id : null;

                    var aircraftRegister = aircraftRegisters.GetValueOrDefault(
                        sourceEntity.GI_AircraftRegister.Code, null);
                    var aircraftRegisterId = aircraftRegister != null ? (Guid?)aircraftRegister.Id : null;

                    var repetitionReasonCode = sourceEntity.LegNumber;

                    var scheduledDepartureDateTime = sourceEntity.Scheduled_DepartureDatetime_UTC;

                    var searchingKey = string.Format("{0},{1},{2},{3}", flightNumberId, aircraftRegisterId, repetitionReasonCode, scheduledDepartureDateTime);
                    var targetEntity = existingFlightLegsOfMonthBucket.GetValueOrDefault(searchingKey, null);

                    if (targetEntity == null)
                    {
                        targetEntity = new FlightLeg();
                        ((IXafEntityObject)targetEntity).OnCreated();
                        infrastructureDb.Set <FlightLeg>().Add(targetEntity);
                        existingFlightLegsOfMonthBucket[searchingKey] = targetEntity;
                        insertCount++;
                    }
                    else
                    {
                        updateCount++;
                    }

                    var flightType = FlightType.All.FirstOrDefault(f => f.Code == sourceEntity.FLT_FlightType.Code);
                    targetEntity.FlightTypeId = flightType != null ? (Guid?)flightType.Id : null;

                    targetEntity.FlightNumberId = flightNumberId;

                    targetEntity.AircraftRegisterId = aircraftRegisterId;

                    targetEntity.Status = sourceEntity.Status;

                    targetEntity.ScheduledDepartureDateTime = new TralusDateTime()
                    {
                        DateTimeUtc = sourceEntity.Scheduled_DepartureDatetime_UTC
                    };
                    targetEntity.EstimatedDepartureDateTime = new TralusDateTime();
                    targetEntity.ActualDepartureDateTime    = new TralusDateTime()
                    {
                        DateTimeUtc = sourceEntity.ACTUAL_DepartureDATETIME_UTC
                    };
                    targetEntity.EstimatedArrivalDateTime = new TralusDateTime();
                    targetEntity.ActualArrivalDateTime    = new TralusDateTime()
                    {
                        DateTimeUtc = sourceEntity.Arrival_ACTUAL_DATETIME_UTC
                    };
                    targetEntity.TakeOffDateTime = new TralusDateTime()
                    {
                        DateTimeUtc = sourceEntity.Takeoff_Datetime
                    };

                    var leg = legs.GetValueOrDefault(sourceEntity.FLT_Route_FlightLeg.Name, null);
                    targetEntity.LegId = leg != null ? (Guid?)leg.Id : null;

                    var arrivlaAirport = airports.GetValueOrDefault(
                        sourceEntity.GI_Airport_ARRIVAL.IATA_AirportCode, null);
                    targetEntity.ArrivalAirportId = arrivlaAirport != null ? (Guid?)arrivlaAirport.Id : null;

                    var departureAirport =
                        airports.GetValueOrDefault(sourceEntity.GI_Airport_DEPARTURE.IATA_AirportCode, null);
                    targetEntity.DepartureAirportId = departureAirport != null ? (Guid?)departureAirport.Id : null;

                    var aircraftType = aircraftTypes.GetValueOrDefault(sourceEntity.GI_AircraftType.IATACode, null);
                    targetEntity.ScheduledAircraftTypeId = aircraftType != null ? (Guid?)aircraftType.Id : null;

                    targetEntity.RepetitionReasonCode = sourceEntity.LegNumber;
                }

                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();
            }
        }
Example #5
0
 public ForbiddenService(InfrastructureDbContext dbContext) =>
Example #6
0
 public ChannelOptionsController(InfrastructureDbContext dbContext)
 {
     _dbContext = dbContext;
 }
Example #7
0
 public CountryService(InfrastructureDbContext dbContext) =>
Example #8
0
 public ConfirmationService(InfrastructureDbContext dbContext) =>
Example #9
0
 public TimezoneService(InfrastructureDbContext dbContext) =>