public static void UseDataBase(this IApplicationBuilder app, IServiceProvider serviceProvider)
        {
            StoreContext storeContext = serviceProvider.GetRequiredService <StoreContext>();

            storeContext.Database.Migrate();

            StoreDbInitializer applicationDbInitializer = serviceProvider.GetRequiredService <StoreDbInitializer>();

            applicationDbInitializer.Initialize().Wait();
        }
Beispiel #2
0
        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                var context  = services.GetRequiredService <StoreDbContext>();
                StoreDbInitializer.Initialize(context);
            }

            host.Run();
        }
Beispiel #3
0
        public static void Main(string[] args)
        {
            var host = CreateWebHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                try
                {
                    var context = scope.ServiceProvider.GetService <StoreDbContext>();

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

            host.Run();
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            var options = new DbContextOptionsBuilder <StoreDbContext>()
                          .UseLazyLoadingProxies()
                          .UseSqlServer(connectionString)
                          .Options;

            var context = new StoreDbContext(options);

            context.Database.EnsureDeleted();
            StoreDbInitializer.Initialize(context);


            var m1 = context.Manufacturers.Find(1);
            var m2 = context.Manufacturers.FirstAsync(x => x.ManufacturerId == 2).Result;

            var d3 = context.Orders.Where(x => x.OrderId == 2).Select(x => x.Customer.FirstName);

            var allOrders = context.Orders.ToListAsync().Result;


            Console.ReadKey();
        }