public async Task <T> AddAsync(T entity)
        {
            _vehicleContext.Set <T>().Add(entity);
            await _vehicleContext.SaveChangesAsync();

            return(entity);
        }
Exemple #2
0
        public static async Task SeedAsync(VehicleDbContext vehicleContext,
                                           UserManager <ApplicationUser> userManager,
                                           ILoggerFactory loggerFactory)
        {
            // Uncomment these two lines to delete and recreate the database
            //vehicleContext.Database.EnsureDeleted();
            //vehicleContext.Database.Migrate();

            // Create Vehicle Statuses
            if (!vehicleContext.VehicleStatuses.Any())
            {
                vehicleContext.VehicleStatuses.AddRange(GetDefaultVehicleStatuses());

                await vehicleContext.SaveChangesAsync();
            }

            // Create Manufacturers
            if (!vehicleContext.Manufacturers.Any())
            {
                vehicleContext.Manufacturers.AddRange(GetDefaultManufacturers());

                await vehicleContext.SaveChangesAsync();
            }

            // Create Vehicle Models
            if (!vehicleContext.VehicleModels.Any())
            {
                vehicleContext.VehicleModels.AddRange(GetDefaultVehicleModels());

                await vehicleContext.SaveChangesAsync();
            }

            // Create Model Years
            if (!vehicleContext.Years.Any())
            {
                vehicleContext.Years.AddRange(GetDefaultModelYears());

                await vehicleContext.SaveChangesAsync();
            }

            // Create Colors
            if (!vehicleContext.Colors.Any())
            {
                vehicleContext.Colors.AddRange(GetDefaultColors());

                await vehicleContext.SaveChangesAsync();
            }

            // Create ModelColor junction table records
            if (!vehicleContext.ColorYears.Any())
            {
                vehicleContext.ColorYears.AddRange(GetModelColorYears(vehicleContext));

                await vehicleContext.SaveChangesAsync();
            }

            // Create default transmissions
            if (!vehicleContext.Transmissions.Any())
            {
                vehicleContext.Transmissions.AddRange(GetTransmissions());

                await vehicleContext.SaveChangesAsync();
            }

            // Create Model_Transmission junction table records
            if (!vehicleContext.ModelTransmissions.Any())
            {
                vehicleContext.ModelTransmissions.AddRange(GetModelYearTransmissions(vehicleContext));

                await vehicleContext.SaveChangesAsync();
            }

            if (!vehicleContext.VehicleLocations.Any())
            {
                vehicleContext.VehicleLocations.AddRange(GetDefaultLocations());
                await vehicleContext.SaveChangesAsync();
            }

            if (!vehicleContext.Images.Any())
            {
                vehicleContext.Images.AddRange(GetDefaultProfilePhotoUri(userManager));
                await vehicleContext.SaveChangesAsync();
            }

            // Combine everything to create some test vehicles
            if (!vehicleContext.Vehicles.Any())
            {
                vehicleContext.Vehicles.AddRange(GetDefaultVehicles(vehicleContext, userManager));

                await vehicleContext.SaveChangesAsync();
            }
        }