private static void InitDataBase(IVideoclubDbContext universidadDbContext, ILogger <Startup> logger)
        {
            var attemps   = 6;
            var isSuccess = false;

            while (!isSuccess && attemps > 0)
            {
                try
                {
                    universidadDbContext.EnsureCreated();
                    isSuccess = true;
                }
                catch (Exception e)
                {
                    Task.Delay(4000).Wait();
                    if (attemps == 0)
                    {
                        logger.LogError(e, "No se pudo inicializar la base de datos");
                        throw;
                    }
                }
                finally
                {
                    attemps--;
                }
            }

            DbInitializer.Initialize(universidadDbContext);

            logger.LogInformation("Se puede inicializar la base de datos correctamente");
        }
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IVideoclubDbContext universidadDbContext, ILogger <Startup> logger)
        {
            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints => endpoints.MapControllers());

            InitDataBase(universidadDbContext, logger);
        }
Exemple #3
0
        public static void Initialize(IVideoclubDbContext context)
        {
            if (context.Rents.Any())
            {
                return;
            }

            context.Rents.Add(new Rent(Guid.Parse("b172a2ab-5900-4532-bd68-68a041752017"), Guid.Parse("5d65ac9e-d431-4138-a8e4-c4719205cb1b"), Status.Rentend, new DateTime(2020, 10, 1)));

            context.SaveChanges();
        }
 private async Task <Rent> GetRent(IVideoclubDbContext ctx, Guid productId, Guid clientId)
 => await ctx.Rents.Where(i => i.ClientId == clientId && i.ProductId == productId)
 .FirstOrDefaultAsync()
 .ConfigureAwait(false);