private static async Task <MarkerInfo> GetMarkersFromWebServiceAsync(IGeotekRepository <MarkerInfo> geotekRepository)
 {
     return(await geotekRepository.GetDataAsync());
 }
コード例 #2
0
 public HomeController(IGeotekRepository <TruckPositionInfo> geotekRepo,
                       IVehicleRepository vehicleRepository)
 {
     _geotekRepo        = geotekRepo;
     _vehicleRepository = vehicleRepository;
 }
        public static void Seed(AppDbContext context,
                                IGeotekRepository <MarkerInfo> markerRepository,
                                IGeotekRepository <TruckPositionInfo> positionRepository,
                                bool raiseException)
        {
            ////Recreation of database each time
            //context.Database.EnsureDeleted();
            //context.Database.EnsureCreated();

            if (context.Vehicles.Any())
            {
                return;
            }


            #region Vehicle Initialization
            // Seed Vehicles
            // don't use here await, because it's not recomended to do so when you dealing with data initialization
            // data seeding must be synchronious
            Task <MarkerInfo> markerTask = GetMarkersFromWebServiceAsync(markerRepository);
            Vehicle[]         vehicles   = ConvertTrackersToVehicles(markerTask.Result.Trackers);

            // Add vehicles to Context
            context.Vehicles.AddRange(vehicles);
            context.SaveChanges();

            #endregion

            var random = new Random();
            #region MotorOil Initialization
            // Seed some MotorOils
            var motorOils = new MotorOil[]
            {
                new MotorOil()
                {
                    Litre = random.Next(100, 200), Manufacturer = "Mobil"
                },
                new MotorOil()
                {
                    Litre = random.Next(100, 200), Manufacturer = "Valvoline"
                },
                new MotorOil()
                {
                    Litre = random.Next(100, 200), Manufacturer = "Mobil"
                },
                new MotorOil()
                {
                    Litre = random.Next(100, 200), Manufacturer = "Shell "
                },
                new MotorOil()
                {
                    Litre = random.Next(100, 200), Manufacturer = "Royal "
                }
            };

            context.MotorOil.AddRange(motorOils);
            context.SaveChanges();

            #endregion

            // Create some services to these vehicles

            // I pull all the vehicles from db to get IDs which have been generated by SQL Server
            // (this is why I don't use local 'vehicles' variable, it has no IDs)
            var vehicleList = context.Vehicles.AsNoTracking().ToList();
            Task <TruckPositionInfo> truckPositionInfoTask = positionRepository.GetDataAsync();
            OilChangeService[]       oilServices           = CreateOilServices(vehicleList, truckPositionInfoTask.Result.Statuses, raiseException);
            context.OilServices.AddRange(oilServices);
            context.SaveChanges();
            Console.WriteLine("Thank you God for not raising Exceptions!");
        }