/// <summary>
        /// Application entry.
        /// </summary>
        /// <param name="args">Arguments.</param>
        public static void Main(string[] args)
        {
            IWebHost host = CreateWebHostBuilder(args).Build();

            using (IServiceScope scope = host.Services.CreateScope())
            {
                try
                {
                    ProductsWebApiContext context = scope.ServiceProvider.GetService <ProductsWebApiContext>();
                    if (context.Database.IsSqlServer())
                    {
                        context.Database.Migrate();
                    }

                    ProductsWebApiInitializer.Initialize(context);
                }
                catch (Exception ex)
                {
                    ILogger <Program> logger = scope.ServiceProvider.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred while migrating or initializing the database.");
                }
            }

            host.Run();
        }
        public static ProductsWebApiContext GetDbContext()
        {
            var builder = new DbContextOptionsBuilder <ProductsWebApiContext>();

            builder.UseInMemoryDatabase(Guid.NewGuid().ToString());

            var dbContext = new ProductsWebApiContext(builder.Options);

            return(dbContext);
        }
        public void SeedDbContext()
        {
            //Arrange
            ProductsWebApiContext dbContext = DbContextHelper.GetDbContext();

            //Act
            ProductsWebApiInitializer.Initialize(dbContext);

            // Assert
            dbContext.Products.Count().Should().BeGreaterThan(0);
        }
        private static IProductFacade GetProductsFacade()
        {
            ProductsWebApiContext dbContext = DbContextHelper.GetDbContext();

            ProductsWebApiInitializer.Initialize(dbContext);

            var mappingConfig = new MapperConfiguration(configuration =>
            {
                configuration.AddProfile <ProductMappingProfile>();
            });

            return(new ProductFacade(dbContext, mappingConfig.CreateMapper()));
        }
Beispiel #5
0
 /// <summary>
 /// Ctor.
 /// </summary>
 /// <param name="dbContext">Products dbContext</param>
 /// <param name="mapper">IMapper for mapping db entities to api dtos.</param>
 public ProductFacade(ProductsWebApiContext dbContext, IMapper mapper)
 {
     _dbContext = dbContext;
     _mapper    = mapper;
 }