예제 #1
0
        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (IServiceScope scope = host.Services.CreateScope())
            {
                IServiceProvider services = scope.ServiceProvider;

                try
                {
                    ClothingContext context = services.GetRequiredService <ClothingContext>();
                    context.Database.Migrate();
                    SeedData.Initialize(services);
                }
                catch (Exception e)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(e, "An error occured while seeding the database.");
                }
            }
        }
예제 #2
0
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (var context =
                       new ClothingContext(
                           serviceProvider
                           .GetRequiredService <DbContextOptions <ClothingContext> >()))
            {
                if (context.Clothings.Any())
                {
                    return;
                }

                context.Clothings.AddRange(
                    new Clothing(
                        Sex.Male,
                        Age.Adult,
                        "Boxers",
                        01,
                        "Underbukser",
                        "Sebastians brugte underbukser",
                        123.456,
                        new List <Image>(),
                        new List <Size>(),
                        new List <Colour>()),

                    new Clothing(
                        Sex.Female,
                        Age.Children,
                        "Shoes",
                        02,
                        "Sko",
                        "Kaares løbesko",
                        0.00,
                        new List <Image>(),
                        new List <Size>(),
                        new List <Colour>()),

                    new Clothing(
                        Sex.Male,
                        Age.Adult,
                        "T-shirt",
                        03,
                        "Grim t-shirt",
                        "T-shirts som kun Johannes ville gå med. ",
                        10,
                        new List <Image>(),
                        new List <Size>(),
                        new List <Colour>()),

                    new Clothing(
                        Sex.Male,
                        Age.Adult,
                        "Hat",
                        03,
                        "Slidt hat",
                        "Hat til at gemme bald spots",
                        600,
                        new List <Image>(),
                        new List <Size>(),
                        new List <Colour>())
                    );

                context.SaveChanges();
            }
        }
예제 #3
0
 public ClothingController(ClothingContext clothingContext, ILogger <ClothingController> logger)
 {
     _clothingContext = clothingContext;
     _logger          = logger;
 }
예제 #4
0
        public static void Initialize(ClothingContext context)
        {
            context.Database.EnsureCreated();

            if (context.Products.Any())
            {
                return;
            }

            var products = new Product[]
            {
                new()
                {
                    Name        = "Big Dress",
                    Description = "Big Dress for BIG Women!",
                    Size        = "DAMN!",
                    Colors      = "BLACK",
                    Price       = 999.99,
                    Gender      = Gender.Women,
                    Categories  = ClothingType.Dresses
                },
                new()
                {
                    Name        = "Dat Grass",
                    Description = "It's just grass..",
                    Size        = "One Size",
                    Colors      = "Green",
                    Price       = 156.00,
                    Gender      = Gender.Men,
                    Categories  = ClothingType.Other
                },
                new()
                {
                    Name        = "Swagger Pants",
                    Description = "It's pants just with Swag!",
                    Size        = "XXXXXL (American Size)",
                    Colors      = "Pink",
                    Price       = 99.99,
                    Gender      = Gender.Unisex,
                    Categories  = ClothingType.Pants
                },
                new()
                {
                    Name        = "Womens Panties",
                    Description = "They're very big!",
                    Size        = "XXXXXXXL (Big Boned)",
                    Colors      = "Barf Green",
                    Price       = 5.99,
                    Gender      = Gender.Women,
                    Categories  = ClothingType.Pants
                },
                new()
                {
                    Name        = "Spider Man Cap",
                    Description = "It's Spider Man on a Hat!",
                    Size        = "Age 10",
                    Colors      = "Spider Man Green",
                    Price       = 1.95,
                    Gender      = Gender.Children,
                    Categories  = ClothingType.Accessories
                }
            };

            context.Products.AddRange(products);
            context.SaveChanges();

            var productImages = new ProductImage[]
            {
                new()
                {
                    ProductId         = context.Products.Single(p => p.Name == "Big Dress").ProductId,
                    ImageFileLocation = "test/path/image.jpg"
                },
                new()
                {
                    ProductId         = context.Products.Single(p => p.Name == "Big Dress").ProductId,
                    ImageFileLocation = "test/path/image.jpg"
                },
                new()
                {
                    ProductId         = context.Products.Single(p => p.Name == "Big Dress").ProductId,
                    ImageFileLocation = "test/path/image.jpg"
                }
            };

            context.ProductImages.AddRange(productImages);
            context.SaveChanges();
        }
    }
}