public static bool tryToMigrate(ProyectoPrograWebContext dbcontext, ApplicationDbContext identitycontext) { try { identitycontext.Database.Migrate(); dbcontext.Database.Migrate(); } catch (Exception) { return(false); } return(true); }
public static bool trySeedDefaultData(ProyectoPrograWebContext dbcontext) { try { // SEXES // MALE, FEMALE CreateSexIfNotExists(dbcontext, 1, "Male"); CreateSexIfNotExists(dbcontext, 2, "Female"); // SPECIES // DOG, HAMSTER, CAT, RABBIT CreateSpecieIfNotExists(dbcontext, 1, "Dog"); CreateBreedIfNotExists(dbcontext, 1, 1, "Chihuahua"); CreateBreedIfNotExists(dbcontext, 2, 1, "Dutch Hound"); CreateBreedIfNotExists(dbcontext, 3, 1, "Cocker Spaniel"); CreateSpecieIfNotExists(dbcontext, 2, "Hamster"); CreateBreedIfNotExists(dbcontext, 4, 2, "Golden"); CreateBreedIfNotExists(dbcontext, 5, 2, "Dwarf"); CreateSpecieIfNotExists(dbcontext, 3, "Cat"); CreateBreedIfNotExists(dbcontext, 6, 3, "European"); CreateBreedIfNotExists(dbcontext, 7, 3, "Egyptian"); CreateBreedIfNotExists(dbcontext, 8, 3, "Siberian"); CreateBreedIfNotExists(dbcontext, 8, 3, "Persian"); CreateSpecieIfNotExists(dbcontext, 4, "Rabbit"); CreateBreedIfNotExists(dbcontext, 9, 4, "American Fuzzy Lop"); CreateBreedIfNotExists(dbcontext, 10, 4, "Dutch Biler"); CreateBreedIfNotExists(dbcontext, 11, 4, "Cashmere Lop"); // STATUSES // AVAILABLE, IN ADOPTION PROCESS CreateStatusPetIfNotExists(dbcontext, 1, "Available"); CreateStatusPetIfNotExists(dbcontext, 2, "In Adoption Process"); // ENERGY LEVELS CreateEnergyLevelIfNotExists(dbcontext, 1, "Reserved"); CreateEnergyLevelIfNotExists(dbcontext, 2, "Playful"); CreateEnergyLevelIfNotExists(dbcontext, 3, "Affectionate"); CreateEnergyLevelIfNotExists(dbcontext, 4, "Independent"); CreateEnergyLevelIfNotExists(dbcontext, 5, "Intelligent"); } catch (Exception) { return(false); } return(true); }
private static EnergyLevel CreateEnergyLevelIfNotExists(ProyectoPrograWebContext dbcontext, int Id, string descripcion) { var obj = dbcontext.EnergyLevels.Where(x => x.LevelId == Id); if (!obj.Any()) { EnergyLevel o = new EnergyLevel() { LevelName = descripcion }; dbcontext.EnergyLevels.Add(o); dbcontext.SaveChanges(); return(o); } return(null); }
private static StatusPet CreateStatusPetIfNotExists(ProyectoPrograWebContext dbcontext, int Id, string descripcion) { var obj = dbcontext.StatusPets.Where(x => x.IdStatus == Id); if (!obj.Any()) { StatusPet o = new StatusPet() { NameStatus = descripcion }; dbcontext.StatusPets.Add(o); dbcontext.SaveChanges(); return(o); } return(null); }
private static Sex CreateSexIfNotExists(ProyectoPrograWebContext dbcontext, int Id, string descripcion) { var obj = dbcontext.Sexes.Where(x => x.IdSex == Id); if (!obj.Any()) { Sex o = new Sex() { NameSex = descripcion }; dbcontext.Sexes.Add(o); dbcontext.SaveChanges(); return(o); } return(null); }
private static Breed CreateBreedIfNotExists(ProyectoPrograWebContext dbcontext, int Id, int IdSpecie, string descripcion) { var obj = dbcontext.Breeds.Where(x => x.IdBreed == Id); if (!obj.Any()) { Breed o = new Breed() { IdSpecieRace = IdSpecie, NameBreed = descripcion }; dbcontext.Breeds.Add(o); dbcontext.SaveChanges(); return(o); } return(null); }
public PetsController(ProyectoPrograWebContext dbcontext, IWebHostEnvironment hostEnvironment) { _dbcontext = dbcontext; _hostEnvironment = hostEnvironment; }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ApplicationDbContext appdbcontext, ProyectoPrograWebContext dbcontext, UserManager <ApplicationUser> userManager, RoleManager <IdentityRole> roleManager) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseMigrationsEndPoint(); } else { app.UseExceptionHandler("/Home/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } Init.tryToMigrate(dbcontext, appdbcontext); Init.tryCreateDefaultUsersAndRoles(userManager, roleManager); Init.trySeedDefaultData(dbcontext); app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); endpoints.MapRazorPages(); }); }