Program.cs including:
- EF Core DbContext
- Seed data
- CORS setup for your React dev server
- Minimal API endpoints (CRUD for Customers)
using Microsoft.EntityFrameworkCore;
using MiniFullStackApi.Models;
var builder = WebApplication.CreateBuilder(args);
// ---------------------------
// 1️⃣ Configure services
// ---------------------------
builder.Services.AddControllers(); // optional for minimal APIs
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// DbContext
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
// CORS policy for React dev server (default Vite port 5173)
var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
builder.Services.AddCors(options =>
{
options.AddPolicy(name: MyAllowSpecificOrigins,
policy =>
{
policy.WithOrigins("http://localhost:5173")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
var app = builder.Build();
// ---------------------------
// 2️⃣ Seed database (optional)
// ---------------------------
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
db.Database.EnsureCreated();
if (!db.Customers.Any())
{
db.Customers.AddRange(
new Customer { Name = "Alice", Region = "North" },
new Customer { Name = "Bob", Region = "South" },
new Customer { Name = "Charlie", Region = "East" }
);
db.SaveChanges();
}
}
// ---------------------------
// 3️⃣ Configure middleware
// ---------------------------
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.UseCors(MyAllowSpecificOrigins);
// ---------------------------
// 4️⃣ Minimal API endpoints
// ---------------------------
// Get all customers
app.MapGet("/customers", async (AppDbContext db) =>
{
return await db.Customers.Include(c => c.Orders).ToListAsync();
});
// Get single customer
app.MapGet("/customers/{id:int}", async (int id, AppDbContext db) =>
{
var customer = await db.Customers.Include(c => c.Orders).FirstOrDefaultAsync(c => c.Id == id);
return customer is not null ? Results.Ok(customer) : Results.NotFound();
});
// Create new customer
app.MapPost("/customers", async (Customer customer, AppDbContext db) =>
{
db.Customers.Add(customer);
await db.SaveChangesAsync();
return Results.Created($"/customers/{customer.Id}", customer);
});
// Update customer
app.MapPut("/customers/{id:int}", async (int id, Customer updatedCustomer, AppDbContext db) =>
{
var customer = await db.Customers.FindAsync(id);
if (customer is null) return Results.NotFound();
customer.Name = updatedCustomer.Name;
customer.Region = updatedCustomer.Region;
await db.SaveChangesAsync();
return Results.Ok(customer);
});
// Delete customer
app.MapDelete("/customers/{id:int}", async (int id, AppDbContext db) =>
{
var customer = await db.Customers.FindAsync(id);
if (customer is null) return Results.NotFound();
db.Customers.Remove(customer);
await db.SaveChangesAsync();
return Results.Ok(customer);
});
app.Run();- CORS setup: React dev server on
http://localhost:5173can now call your API. - EF DbContext: Already wired to SQL Server using your connection string.
- Endpoints: CRUD fully functional.
- Seed: Creates initial customers if none exist.