Skip to content
Snippets Groups Projects
Program.cs 1.6 KiB
Newer Older
using Microsoft.EntityFrameworkCore;
using BookingCatalog.Data;
using BookingCatalog.RabbitMQ;

var builder = WebApplication.CreateBuilder(args);

Nivethan1999's avatar
Nivethan1999 committed
// var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
// Console.WriteLine($"Connection string: {connectionString}");
// Add services to the container.
Nivethan1999's avatar
Nivethan1999 committed
// 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();
Nivethan1999's avatar
Nivethan1999 committed


builder.Services.AddDbContext<BookingCatalogDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
var app = builder.Build();
Nivethan1999's avatar
Nivethan1999 committed
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();
Nivethan1999's avatar
Nivethan1999 committed

Nivethan1999's avatar
Nivethan1999 committed
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");
   }

}
Nivethan1999's avatar
Nivethan1999 committed

app.Run();