0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

コード

Program.cs
using InsuranceProductManager;
using InsuranceProductManager.Models; // ← Product を使うので追加
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// 1. 必要なサービスを登録(Controllers, Swagger, DbContextなど)
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// ✅ AppDbContext を登録(必要に応じて接続文字列を調整)
builder.Services.AddDbContext<AppDbContext>(options =>
    options.UseInMemoryDatabase("InsuranceProductsDB")); // SQLite や SQL Server に変更も可

var app = builder.Build();

// --- 起動時にサンプルデータを投入 ---
using (var scope = app.Services.CreateScope())
{
    var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();

    // データが空の場合のみ追加(重複防止)
    if (!db.Products.Any())
    {
        // シードデータ
        db.Products.AddRange(
            new Product { Name = "保険A", Description = "説明A", Price = 1000, IsPrivate = false },
            new Product { Name = "保険B", Description = "説明B", Price = 2000, IsPrivate = true }
        );
        db.SaveChanges();
    }
}

// 2. ミドルウェアの設定
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

// 3. コントローラールートのマッピング
app.MapControllers();

app.Run();

UI

スクリーンショット 2025-08-09 7.02.43.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?