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(); }
public static void Main(string[] args) { var host = CreateWebHostBuilder(args).Build(); using (var scope = host.Services.CreateScope()) //lager et scope som begrenser levetiden til en variabel { var services = scope.ServiceProvider; var context = services.GetRequiredService <StoreDbContext>(); StoreDbInitializer.Initilaizer(context); } host.Run();//vil kjøre en database før applikasjonene kjører }
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(); }
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(); }
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(); }