Пример #1
0
        private void Seed()
        {
            var context = new EFUnitOfWork(_contextBuilder.Options);

            // Ensure to clear data if exists
            if (context.CreateSet <Flight>().Count() > 0)
            {
                context.CreateSet <Flight>().ToList().ForEach(record =>
                {
                    context.Remove(record);
                });
            }

            if (context.CreateSet <Aircraft>().Count() > 0)
            {
                context.CreateSet <Aircraft>().ToList().ForEach(record =>
                {
                    context.Remove(record);
                });
            }

            if (context.CreateSet <Airport>().Count() > 0)
            {
                context.CreateSet <Airport>().ToList().ForEach(record =>
                {
                    context.Remove(record);
                });
            }
            // Create Fake Records

            Random random = new Random();

            _airportsRecords  = new List <Airport>();
            _aircraftsRecords = new List <Aircraft>();
            _flightsRecords   = new List <Flight>();

            for (int i = 1; i < 10; i++)
            {
                _airportsRecords.Add(new Airport
                {
                    Name      = "FakeAirport_" + i,
                    Latitude  = random.Next(1000, 99999),
                    Longitude = random.Next(1000, 99999)
                });
            }

            for (int i = 1; i < 10; i++)
            {
                _aircraftsRecords.Add(new Aircraft
                {
                    Code              = "FakeAircraft_" + i,
                    MilesPerHour      = random.Next(100, 300),
                    TakeOffEffort     = random.Next(30, 50),
                    ConsumptionKgPerH = random.Next(300, 800)
                });
            }

            for (int i = 1; i < 10; i++)
            {
                _flightsRecords.Add(new Flight
                {
                    FlightNumber         = "FakeFlight_" + i,
                    AirportDepartureId   = i,
                    AirportDestinationId = i + 1,
                    AircraftId           = i,
                    FlightDate           = DateTime.Now,
                    FlightTime           = DateTime.Now
                });
            }

            context.AddRange(_airportsRecords);
            context.AddRange(_aircraftsRecords);
            context.AddRange(_flightsRecords);
            context.SaveChanges();
        }