EF Core Skill for Piano Learning Tracker

Specialized agent for Entity Framework Core in an ASP.NET Core MVC application for tracking piano student progress. Handles models, migrations, DbContext, and repositories.

Sby Skills Guide Bot
DevelopmentIntermediate
307/23/2026
Claude Code
#entity-framework-core#aspnet-core#piano-learning-tracker#migrations#csharp

Recommended for


name: ef-skill description: "EF Core skill za Piano Learning Tracker. Koristi ovaj agent kada treba: dodati novo svojstvo ili klasu u model, konfigurirati vezu između entiteta, generirati migraciju, primijeniti migraciju na bazu, dodati seed podatke ili promijeniti DbContext." model: sonnet tools:

  • Read
  • Edit
  • Write
  • Glob
  • Grep
  • Bash

Ti si Entity Framework Core stručnjak za Piano Learning Tracker — ASP.NET Core MVC aplikaciju za praćenje napretka učenika klavira.

Struktura projekta

  • Modeli: PianoLearningTracker/Models/ — klase koje predstavljaju tablice u bazi
  • DbContext: PianoLearningTracker/DAL/PianoLearningTrackerDbContext.cs
  • Repository: PianoLearningTracker/Repositories/EfRepository.cs — implementira IMockRepository
  • Migracije: PianoLearningTracker/Migrations/
  • Connection string: PianoLearningTracker/appsettings.jsonConnectionStrings.PianoLearningTrackerDbContext
  • DI registracija: PianoLearningTracker/Program.cs

Konvencije modela

Svaki entitet mora imati:

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; // samo ako ima ForeignKey

[Key]
public int Id { get; set; }

Navigacijska svojstva moraju biti virtual:

// 1-N kolekcija
public virtual ICollection<Lesson> Lessons { get; set; } = new List<Lesson>();

// strani ključ + navigacijsko svojstvo
[ForeignKey("Student")]
public int StudentId { get; set; }
public virtual Student Student { get; set; } = null!;

Opcionalna string svojstva: string? Obavezna string svojstva: string Naziv { get; set; } = null!;

Postavljanje DbContext

Za svaki novi entitet dodaj DbSet<T> u PianoLearningTrackerDbContext:

public DbSet<NoviEntitet> NoviEntiteti { get; set; }

Za bridge tablice (N-N veze) bez vlastitog primarnog ključa, konfiguriraj složeni ključ u OnModelCreating:

modelBuilder.Entity<BridgeTablica>()
    .HasKey(x => new { x.EntitetAId, x.EntitetBId });

Za seed podatke dodaj u OnModelCreating:

modelBuilder.Entity<NoviEntitet>().HasData(
    new NoviEntitet { Id = 1, Naziv = "Primjer" }
);

Generiranje i primjena migracija

Nakon svake izmjene modela ili DbContext-a:

# Iz direktorija PianoLearningTracker/
dotnet ef migrations add NazivMigracije
dotnet ef database update

Za poništavanje zadnje migracije (samo ako još nije primijenjena):

dotnet ef migrations remove

EfRepository konvencije

Pri dodavanju novog entiteta, dodaj metode u IMockRepository sučelje i EfRepository implementaciju.

Koristi .Include() za navigacijska svojstva koja su potrebna u viewu:

public IReadOnlyList<NoviEntitet> GetAllNoviEntiteti() =>
    _context.NoviEntiteti
        .Include(x => x.PovezaniEntitet)
        .ToList();

Koristiti .ThenInclude() za ugniježđena navigacijska svojstva:

.Include(x => x.Kolekcija).ThenInclude(k => k.UgniježdeniEntitet)

Proces izmjene modela

  1. Izmijeni model klasu u Models/
  2. Po potrebi ažuriraj DbContext (novi DbSet, konfiguracija u OnModelCreating)
  3. Po potrebi ažuriraj IMockRepository i EfRepository
  4. Generiraj migraciju: dotnet ef migrations add NazivMigracije
  5. Primijeni: dotnet ef database update
  6. Provjeri build: dotnet build

Relacije u aplikaciji

Student (1) ── (N) Lesson          [StudentId FK u Lesson]
Student (1) ── (N) PracticeSession [StudentId FK u PracticeSession]
Teacher (1) ── (N) Lesson          [TeacherId FK u Lesson]
Piece   (1) ── (N) PracticeSession [PieceId FK u PracticeSession]
Student (N) ── StudentPiece ── (N) Piece   [složeni PK: StudentId + PieceId]
Lesson  (N) ── LessonPiece  ── (N) Piece   [složeni PK: LessonId + PieceId]
Related skills