Пример #1
0
        protected override void Seed(TrainTicketsDbContext context)
        {
            var paris = new City {
                Name = "Paris", Id = Guid.Parse("280BB2E9-782A-42C9-875A-7DC0178452A6")
            };
            var brussels = new City {
                Name = "Brussels", Id = Guid.Parse("280BB2E9-782A-42C9-875A-7DC0178452A7")
            };
            var berlin = new City {
                Name = "Berlin", Id = Guid.Parse("280BB2E9-782A-42C9-875A-7DC0178452A8")
            };
            var amsterdam = new City {
                Name = "Amsterdam", Id = Guid.Parse("280BB2E9-782A-42C9-875A-7DC0178452A9")
            };
            var london = new City {
                Name = "London", Id = Guid.Parse("280BB2E9-782A-42C9-875A-7DC0178452AA")
            };

            var cities = new List <City>()
            {
                paris, brussels, berlin, amsterdam, london
            };

            List <TrainRoute> trainRoutes;

            if (context.Cities.Count() == 0)
            {
                context.Cities.AddOrUpdate(cities.ToArray());

                // TrainRoutes
                trainRoutes = new List <TrainRoute>()
                {
                    new TrainRoute {
                        Departure = paris, Arrival = brussels, Price = 20.0M
                    },
                    new TrainRoute {
                        Departure = paris, Arrival = amsterdam, Price = 30.0M
                    },
                    new TrainRoute {
                        Departure = paris, Arrival = berlin, Price = 50.0M
                    },
                    new TrainRoute {
                        Departure = paris, Arrival = london, Price = 40.0M
                    },

                    new TrainRoute {
                        Departure = brussels, Arrival = amsterdam, Price = 20.0M
                    },
                    new TrainRoute {
                        Departure = brussels, Arrival = berlin, Price = 40.0M
                    },
                    new TrainRoute {
                        Departure = brussels, Arrival = london, Price = 30.0M
                    },
                    new TrainRoute {
                        Departure = brussels, Arrival = paris, Price = 30.0M
                    },

                    new TrainRoute {
                        Departure = amsterdam, Arrival = london, Price = 30.0M
                    },
                    new TrainRoute {
                        Departure = amsterdam, Arrival = berlin, Price = 40.0M
                    },
                    new TrainRoute {
                        Departure = amsterdam, Arrival = paris, Price = 30.0M
                    },
                    new TrainRoute {
                        Departure = amsterdam, Arrival = brussels, Price = 20.0M
                    },

                    new TrainRoute {
                        Departure = london, Arrival = berlin, Price = 60.0M
                    },
                    new TrainRoute {
                        Departure = london, Arrival = paris, Price = 40.0M
                    },
                    new TrainRoute {
                        Departure = london, Arrival = amsterdam, Price = 30.0M
                    },
                    new TrainRoute {
                        Departure = london, Arrival = brussels, Price = 30.0M
                    },

                    new TrainRoute {
                        Departure = berlin, Arrival = london, Price = 60.0M
                    },
                    new TrainRoute {
                        Departure = berlin, Arrival = paris, Price = 50.0M
                    },
                    new TrainRoute {
                        Departure = berlin, Arrival = amsterdam, Price = 40.0M
                    },
                    new TrainRoute {
                        Departure = berlin, Arrival = brussels, Price = 40.0M
                    },
                };

                context.TrainRoutes.AddOrUpdate(trainRoutes.ToArray());
            }
            else
            {
                trainRoutes = context.TrainRoutes.ToList();
            }

            var dates  = DateTimeGenerator.GetDaysInRange(DateTime.Today, DateTime.Today.AddDays(60));
            var trains = new List <Train>();

            foreach (DateTime date in dates)
            {
                for (int i = 0; i < 24; i += 2)
                {
                    foreach (var route in trainRoutes)
                    {
                        trains.Add(TrainGenerator.GenerateTrain(route,
                                                                date.AddHours(i).AddMinutes(rand.Next(0, 60))));
                    }
                }
            }

            context.Trains.AddRange(trains);

            context.Configuration.AutoDetectChangesEnabled = false;
            context.Configuration.ValidateOnSaveEnabled    = false;
            context.SaveChanges();
        }
        private void BusVehicleSeed()
        {
            OperationDataContext context = new OperationDataContext();

            var bandung = new PlaceModel {
                Name = "Bandung"
            };
            var yogyakarta = new PlaceModel {
                Name = "Yogyakarta"
            };
            var curup = new PlaceModel {
                Name = "Curup"
            };
            var surabaya = new PlaceModel {
                Name = "Surabaya"
            };
            var denpasar = new PlaceModel {
                Name = "Denpasar"
            };

            var places = new List <PlaceModel>()
            {
                bandung, yogyakarta, curup, surabaya, denpasar
            };

            List <RouteModel> busRoutes = new List <RouteModel>();

            // Generate list of busRoute
            foreach (var origin in places)
            {
                foreach (var destination in places)
                {
                    if (origin != destination)
                    {
                        busRoutes.Add(new RouteModel {
                            OriginName = origin.Name, DestinationName = destination.Name
                        });
                    }
                }
            }

            if (context.Places.Count() == 0)
            {
                // Insert all places above to database
                foreach (var item in places)
                {
                    Place place = new Place()
                    {
                        Name = item.Name
                    };

                    context.Places.InsertOnSubmit(place);
                    context.SubmitChanges();
                }

                // Insert all busRoute to database
                foreach (var item in busRoutes)
                {
                    Route route = new Route()
                    {
                        Origin      = item.OriginName,
                        Destination = item.DestinationName
                    };

                    context.Routes.InsertOnSubmit(route);
                    context.SubmitChanges();
                }
            }

            if (context.BusVehicles.Count() == 0)
            {
                var dates  = DateTimeGenerator.GetDaysInRange(DateTime.Today, DateTime.Today.AddDays(60));
                var busses = new List <BusVehicleModel>();

                var busNames = new List <string>()
                {
                    "Primajasa", "MGI", "Budiman", "Putra Raflesia", "Lorena", "Haryanto"
                };
                var busClasses = AppConstants.BusClasses;

                foreach (DateTime date in dates)
                {
                    //var tes = date;
                    for (double i = 0; i < 24; i += 2)
                    {
                        foreach (var route in busRoutes)
                        {
                            int randName  = rand.Next(busNames.Count);
                            int randClass = rand.Next(busClasses.Count);

                            busses.Add(BusGenerator.GenerateBus(route,
                                                                date.AddHours(rand.Next(0, 24)).AddMinutes(rand.Next(0, 60)),
                                                                busNames[randName],
                                                                busClasses[randClass]));
                        }
                    }
                }

                // Insert all busvehicle to database
                // Belum bisa, masih ada errornya
                foreach (var item in busses)
                {
                    BusVehicle busVehicle = new BusVehicle()
                    {
                        Name          = item.Name,
                        Class         = item.Class,
                        DepartureTime = DateTime.Parse(item.DepartureTime),
                        RouteID       = item.RouteID
                    };

                    //context.BusVehicles.InsertOnSubmit(busVehicle);
                    //context.SubmitChanges();
                }
            }
        }