using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; var builder = WebApplication.CreateBuilder(args); // Configure the database builder.Services.AddDbContext(options => options.UseSqlite("Data Source=users.db")); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); // Seed the database using (var scope = app.Services.CreateScope()) { var db = scope.ServiceProvider.GetRequiredService(); db.Database.Migrate(); if (!db.Users.Any()) { db.Users.AddRange(new User { Name = "Alice" }, new User { Name = "Bob" }); db.SaveChanges(); } } // Define API endpoints app.MapGet("/users", async (AppDbContext db) => await db.Users.ToListAsync()); app.Run(); // Database context class AppDbContext : DbContext { public AppDbContext(DbContextOptions options) : base(options) { } public DbSet Users => Set(); } // User model class User { public int Id { get; set; } public string Name { get; set; } }