| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- using Microsoft.EntityFrameworkCore;
- using Recepie.Models;
- namespace Recepie.Data
- {
- public class RecipeContext : DbContext
- {
- public RecipeContext(DbContextOptions<RecipeContext> options) : base(options)
- {
- }
- public DbSet<Recipe> Recipes { get; set; }
- public DbSet<Ingredient> Ingredients { get; set; }
- public DbSet<RecipeIngredientLink> RecipeIngredientLinks { get; set; }
- public DbSet<RecipeStep> RecipeSteps { get; set; }
- public DbSet<WeekPlan> WeekPlans { get; set; }
- public DbSet<DayPlan> DayPlans { get; set; }
- // public DbSet<RecipeImage> RecipeImages { get; set; } // Temporarily disabled for main page performance
- protected override void OnModelCreating(ModelBuilder modelBuilder)
- {
- base.OnModelCreating(modelBuilder);
- modelBuilder.Entity<Recipe>(entity =>
- {
- // Map to existing lowercase table name
- entity.ToTable("recepie");
- entity.HasKey(e => e.Id);
- entity.Property(e => e.Id).HasColumnName("id");
- // Map our model properties to your database columns
- entity.Property(e => e.Title).HasColumnName("name").IsRequired();
- entity.Property(e => e.Description).HasColumnName("description");
- entity.Property(e => e.Difficulty).HasColumnName("difficulty");
- entity.Property(e => e.Url).HasColumnName("url");
- entity.Property(e => e.Time).HasColumnName("time");
- // Index for better search performance
- entity.HasIndex(e => e.Title);
- });
- modelBuilder.Entity<Ingredient>(entity =>
- {
- entity.ToTable("ingredient");
- entity.HasKey(e => e.Id);
- entity.Property(e => e.Id).HasColumnName("id");
- entity.Property(e => e.Name).HasColumnName("name");
- });
- modelBuilder.Entity<RecipeIngredientLink>(entity =>
- {
- entity.ToTable("recepieIngredientLink");
- entity.HasKey(e => new { e.RecipeId, e.IngredientId });
- entity.Property(e => e.RecipeId).HasColumnName("recepieId");
- entity.Property(e => e.IngredientId).HasColumnName("ingredientId");
- entity.Property(e => e.Amount).HasColumnName("amount");
- entity.Property(e => e.Measurement).HasColumnName("measurement");
- // Configure relationships
- entity.HasOne(e => e.Recipe)
- .WithMany(r => r.RecipeIngredients)
- .HasForeignKey(e => e.RecipeId);
- entity.HasOne(e => e.Ingredient)
- .WithMany()
- .HasForeignKey(e => e.IngredientId);
- });
- modelBuilder.Entity<RecipeStep>(entity =>
- {
- entity.ToTable("recepieSteps");
- entity.HasKey(e => e.Id);
- entity.Property(e => e.Id).HasColumnName("id");
- entity.Property(e => e.RecipeId).HasColumnName("recepieId");
- entity.Property(e => e.StepNumber).HasColumnName("stepNumber");
- entity.Property(e => e.Text).HasColumnName("text");
- // Configure relationship
- entity.HasOne(e => e.Recipe)
- .WithMany(r => r.RecipeSteps)
- .HasForeignKey(e => e.RecipeId);
- });
- // Week Planner Configuration
- modelBuilder.Entity<WeekPlan>(entity =>
- {
- entity.ToTable("weekplan");
- entity.HasKey(e => e.Id);
- entity.Property(e => e.Id).HasColumnName("id");
- entity.Property(e => e.Name).HasColumnName("name").HasMaxLength(255);
- entity.Property(e => e.StartDate).HasColumnName("startdate");
- entity.Property(e => e.CreatedAt).HasColumnName("createdat");
- });
- modelBuilder.Entity<DayPlan>(entity =>
- {
- entity.ToTable("dayplan");
- entity.HasKey(e => e.Id);
- entity.Property(e => e.Id).HasColumnName("id");
- entity.Property(e => e.WeekPlanId).HasColumnName("weekplanid");
- entity.Property(e => e.DayOfWeek).HasColumnName("dayofweek");
- entity.Property(e => e.Date).HasColumnName("date");
- entity.Property(e => e.RecipeId).HasColumnName("recipeid");
- entity.Property(e => e.MainIngredient).HasColumnName("mainingredient").HasMaxLength(100);
- entity.Property(e => e.BannedIngredients).HasColumnName("bannedingredients").HasMaxLength(500);
- entity.Property(e => e.RequiredIngredients).HasColumnName("requiredingredients").HasMaxLength(500);
- entity.HasOne(d => d.WeekPlan)
- .WithMany(p => p.DayPlans)
- .HasForeignKey(d => d.WeekPlanId);
- entity.HasOne(d => d.Recipe)
- .WithMany()
- .HasForeignKey(d => d.RecipeId);
- });
- // Temporarily disabled RecipeImage configuration for performance
- /*
- modelBuilder.Entity<RecipeImage>(entity =>
- {
- entity.ToTable("recepieImage");
- entity.HasKey(e => e.RecipeId);
- entity.Property(e => e.RecipeId).HasColumnName("recepieId");
- entity.Property(e => e.ImageData).HasColumnName("image");
- // Configure relationship
- entity.HasOne(e => e.Recipe)
- .WithOne(r => r.RecipeImage)
- .HasForeignKey<RecipeImage>(e => e.RecipeId);
- });
- */
- }
- }
- }
|