コード例 #1
0
ファイル: Program.cs プロジェクト: pcmichaels/UsefulSites
        static void Main(string[] args)
        {
            Console.WriteLine("This tool will clear and then repopulate the data in the database.");
            Console.WriteLine("Press Y to continue, or any other key to cancel.");

            ConsoleKeyInfo key = Console.ReadKey();

            if (key.Key != ConsoleKey.Y)
            {
                return;
            }

            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

            IConfigurationRoot configuration = builder.Build();

            Console.WriteLine(configuration.GetConnectionString("DefaultConnection"));

            var options =
                new DbContextOptionsBuilder <ApplicationDbContext>()
                .UseSqlServer(configuration.GetConnectionString("DefaultConnection"))
                .EnableSensitiveDataLogging()
                .Options;
            ApplicationDbContext applicationDbContext = new ApplicationDbContext(options);

            var dataAccess = new ResourceDataAccess(applicationDbContext);
            ResourceCategoryDataAccess resourceCategoryDataAccess = new ResourceCategoryDataAccess(applicationDbContext);

            int[] categories = GetCategories(resourceCategoryDataAccess);

            for (int i = 1; i <= 100; i++)
            {
                int    category       = _random.Next(categories.Length);
                string webSiteAddress = $"www.{CreateWebAddress()}.{GetExtension()}";

                dataAccess.CreateWebSite(categories[category], $"Test web site {i}", webSiteAddress);
            }
        }