Newer
Older
using Microsoft.EntityFrameworkCore;
using BookingCatalog.Data;
var builder = WebApplication.CreateBuilder(args);
// var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
// Console.WriteLine($"Connection string: {connectionString}");
// builder.Services.AddDbContext<BookingCatalogDbContext>(
// options => options.UseSqlServer(connectionString)
// );
builder.Services.AddScoped<BookingService>();
builder.Services.AddSingleton<RabbitMqService>();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddControllers();
builder.Services.AddDbContext<BookingCatalogDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<BookingCatalogDbContext>();
db.Database.Migrate();
}
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
var context = services.GetRequiredService<BookingCatalogDbContext>();
if (context != null)
{
SeedData.Seed(context);
}
else
{
throw new Exception("Cant reach BookingCatalogDbContext");
}
}