DeutschLernen/GermanApp/Infrastructure/Data/DbContext/AppDbContext.cs
Lasse Rune Hansen d90e4792d6 Initial commit: GermanApp with Clean Architecture
- Domain layer: Lesson entity, GermanWord value object, repository interfaces
- Application layer: CQRS commands, DTOs with mapping
- Infrastructure layer: EF Core with SQLite, LessonRepository
- Presentation layer: Minimal API endpoints for lessons CRUD

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-05-31 18:14:51 +02:00

54 lines
2.3 KiB
C#

using GermanApp.Domain.Entities;
using Microsoft.EntityFrameworkCore;
namespace GermanApp.Infrastructure.Data.DbContext;
/// <summary>
/// Entity Framework Core database context.
/// This is part of the Infrastructure layer.
/// </summary>
public class AppDbContext : Microsoft.EntityFrameworkCore.DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
// DbSets for domain entities
public DbSet<Lesson> Lessons { get; set; } = null!;
// Note: Value objects are not stored directly as entities.
// They are owned by entities and stored as part of the entity's data.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Configure Lesson entity
modelBuilder.Entity<Lesson>(builder =>
{
builder.HasKey(l => l.Id);
builder.Property(l => l.Title).IsRequired().HasMaxLength(200);
builder.Property(l => l.Description).IsRequired().HasMaxLength(2000);
builder.Property(l => l.Level).IsRequired();
builder.Property(l => l.CreatedAt).IsRequired();
builder.Property(l => l.UpdatedAt).IsRequired(false);
// Value object: GermanWord would be configured as an owned entity
// builder.OwnsMany(l => l.Words, wordBuilder =>
// {
// wordBuilder.Property(w => w.Value).HasColumnName("WordValue");
// wordBuilder.Property(w => w.Translation).HasColumnName("Translation");
// wordBuilder.Property(w => w.Article).HasColumnName("Article");
// wordBuilder.Property(w => w.PartOfSpeech).HasColumnName("PartOfSpeech");
// });
});
// Seed data (optional) - Note: For EF Core, we need to set properties directly
// In a real application, use migrations or a separate seeding mechanism
// modelBuilder.Entity<Lesson>().HasData(
// new { Id = 1, Title = "Greetings", Description = "Basic German greetings", Level = 1, CreatedAt = DateTime.UtcNow },
// new { Id = 2, Title = "Numbers", Description = "German numbers 1-100", Level = 1, CreatedAt = DateTime.UtcNow },
// new { Id = 3, Title = "Grammar Basics", Description = "Basic German grammar", Level = 2, CreatedAt = DateTime.UtcNow }
// );
}
}