private static void SeedTransmissionTypes(GMDbContext dbContext) { var transmissionTypes = new List <TransmissionType> { new TransmissionType { Name = SeedConstants.Manual }, new TransmissionType { Name = SeedConstants.SemiAutomatic }, new TransmissionType { Name = SeedConstants.DirectShiftGearbox }, new TransmissionType { Name = SeedConstants.Automatic } }; dbContext.TransmissionTypes.AddRange(transmissionTypes); dbContext.SaveChanges(); }
private static void SeedDepartments(GMDbContext dbContext) { var departmentList = new List <Department> { new Department { Name = SeedConstants.BodyRepair }, new Department { Name = SeedConstants.FacilitiesManagement }, new Department { Name = SeedConstants.MechanicalRepair }, new Department { Name = SeedConstants.Painting } }; dbContext.Departments.AddRange(departmentList); dbContext.SaveChanges(); }
private static void SeedOptionalData(IServiceScope serviceScope, GMDbContext dbContext) { var userManager = serviceScope.ServiceProvider.GetRequiredService <UserManager <GMUser> >(); var roleManager = serviceScope.ServiceProvider.GetRequiredService <RoleManager <IdentityRole> >(); SeedDefaultRoles(userManager, roleManager, dbContext); SeedCars(dbContext); }
private static void SeedRequiredData(GMDbContext dbContext) { SeedManufacturers(dbContext); SeedModels(dbContext); SeedCustomers(dbContext); SeedFuelTypes(dbContext); SeedTransmissionTypes(dbContext); SeedDepartments(dbContext); }
private static void SeedManufacturers(GMDbContext dbContext) { var manufacturerList = File .ReadAllText(SeedConstants.CarManufacturerDataPath); var manufacturerTypes = JsonConvert.DeserializeObject <VehicleManufacturer[]>(manufacturerList); dbContext.VehicleManufacturers.AddRange(manufacturerTypes); dbContext.SaveChanges(); }
private static void SeedCustomers(GMDbContext dbContext) { var customerList = File .ReadAllText(SeedConstants.CustomerDataPath); var customers = JsonConvert.DeserializeObject <Customer[]>(customerList); dbContext.Customers.AddRange(customers); dbContext.SaveChanges(); }
private static void SeedModels(GMDbContext dbContext) { var modelList = File .ReadAllText(SeedConstants.CarModelDataPath); var models = JsonConvert.DeserializeObject <VehicleModel[]>(modelList); dbContext.VehicleModels.AddRange(models); dbContext.SaveChanges(); }
//public override async Task<IdentityResult> AddToRoleAsync(int userId, string roleId) //{ // //var user = await FindByIdAsync(userId); // GMDbContext context = new GMDbContext(); // var user = context.Users.Find(userId); // var existingRole = user.Roles; // var count = user.Roles.Count(); // for (var i = 0; i < count; i++)// role in existingRole) // { // var result = user.Roles.Remove(existingRole.ElementAt(i)); // context.SaveChanges(); // } // user.Roles.Add(new GMUserRole { RoleId = int.Parse(roleId), UserId = userId }); // context.SaveChanges(); // return new IdentityResult(); //} public override async Task <IdentityResult> AddToRoleAsync(int userId, string roleId) { GMDbContext context = new GMDbContext(); var user = context.Users.Find(userId); user.Roles.Add(new GMUserRole { RoleId = int.Parse(roleId), UserId = userId }); context.SaveChanges(); return(new IdentityResult()); }
public override async Task <IdentityResult> RemoveFromRoleAsync(int userId, string roleId) { //var user = await FindByIdAsync(userId); GMDbContext context = new GMDbContext(); var user = context.Users.Find(userId); var existingRole = user.Roles; var count = user.Roles.Count(); for (var i = 0; i < count; i++) { var result = user.Roles.Remove(existingRole.ElementAt(0)); } ; context.SaveChanges(); return(new IdentityResult()); }
private static void SeedFuelTypes(GMDbContext dbContext) { var fuelTypes = new List <FuelType> { new FuelType { Name = SeedConstants.FuelTypeBioDiesel }, new FuelType { Name = SeedConstants.FuelTypeCNG }, new FuelType { Name = SeedConstants.FuelTypeDiesel }, new FuelType { Name = SeedConstants.FuelTypeElectricity }, new FuelType { Name = SeedConstants.FuelTypeGasoline }, new FuelType { Name = SeedConstants.FuelTypeHydrogen }, new FuelType { Name = SeedConstants.FuelTypeLPG } }; dbContext.FuelTypes.AddRange(fuelTypes); dbContext.SaveChanges(); }
//protected override void Seed(GMDbContext context) //{ // InitializeIdentityForEF(context); // base.Seed(context); //} //Create [email protected] with password=Admin@123456 in the Admin role public static void InitializeIdentityForEF(GMDbContext db) { InitializeIdentityForEF(); }
private static void SeedCars(GMDbContext dbContext) { var random = new Random(); var customerIds = dbContext.Customers .OrderBy(customer => customer.FullName) .Select(customer => customer.Id) .Take(SeedConstants.CountOfCustomersToSeedWithCars) .ToList(); var fuelTypes = dbContext.FuelTypes .Select(type => type.Id) .ToList(); var transmissionTypes = dbContext.TransmissionTypes .Select(type => type.Id) .ToList(); var manufactuters = dbContext.VehicleManufacturers .ToList();; var models = dbContext.VehicleModels .ToList(); var vins = GetCarVins(); var registrationPlates = GetCarRegistrationPlates(); var horsePowerValues = GetCarEngineHorsePowerValues(); var yearsOfManufacturing = GetCarYearsOfManufacturing(); var engineModels = GetCarEnginModels(); var kilometValues = GetCarKilometersValues(); var cars = new List <Car>(); var services = new List <ServiceIntervention>(); foreach (var customerId in customerIds) { for (int i = 0; i < 20; i++) { var manufacturer = manufactuters[random.Next(0, manufactuters.Count - 1)]; var manufacturerModels = models .Where(m => m.ManufactirerId == manufacturer.Id) .ToList(); if (!manufacturerModels.Any()) { continue; } var model = manufacturerModels[random.Next(0, manufacturerModels.Count - 1)]; var fuelTypeId = fuelTypes[random.Next(0, fuelTypes.Count)]; var transmissionId = transmissionTypes[random.Next(0, transmissionTypes.Count - 1)]; var registrationPLate = registrationPlates[random.Next(0, registrationPlates.Count - 1)]; var yearOfManufacturing = yearsOfManufacturing[random.Next(0, yearsOfManufacturing.Count - 1)]; var vin = vins[random.Next(0, vins.Count - 1)]; var horsePoerValue = horsePowerValues[random.Next(0, horsePowerValues.Count - 1)]; var engineModel = engineModels[random.Next(0, engineModels.Count - 1)]; var kilometerVaue = kilometValues[random.Next(0, kilometValues.Count - 1)]; var service = new ServiceIntervention(); var car = new Car { CustomerId = customerId, Vin = vin, RegistrationPlate = registrationPLate, MakeId = manufacturer.Id, ModelId = model.Id, FuelTypeId = fuelTypeId, TransmissionId = transmissionId, YearOfManufacturing = yearOfManufacturing, EngineHorsePower = horsePoerValue, EngineModel = engineModel, Кilometers = kilometerVaue, ServiceId = service.Id }; service.CarId = car.Id; cars.Add(car); services.Add(service); } } dbContext.ServiceInterventions.AddRange(services); dbContext.Cars.AddRange(cars); dbContext.SaveChanges(); }
private static void SeedDefaultRoles(UserManager <GMUser> userManager, RoleManager <IdentityRole> roleManager, GMDbContext dbContext) { Task .Run(async() => { var roles = new[] { RoleConstants.AdministratorRoleName, RoleConstants.EmployeeRoleName }; foreach (var role in roles) { var roleExist = await roleManager.RoleExistsAsync(role); if (!roleExist) { await roleManager.CreateAsync(new IdentityRole(role)); } } }).GetAwaiter().GetResult(); }
public DeletableEntityRepository(GMDbContext dbContext) { this.dbContext = dbContext; }