RecipeContext.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using Microsoft.EntityFrameworkCore;
  2. using Recepie.Models;
  3. namespace Recepie.Data
  4. {
  5. public class RecipeContext : DbContext
  6. {
  7. public RecipeContext(DbContextOptions<RecipeContext> options) : base(options)
  8. {
  9. }
  10. public DbSet<Recipe> Recipes { get; set; }
  11. public DbSet<Ingredient> Ingredients { get; set; }
  12. public DbSet<RecipeIngredientLink> RecipeIngredientLinks { get; set; }
  13. public DbSet<RecipeStep> RecipeSteps { get; set; }
  14. public DbSet<WeekPlan> WeekPlans { get; set; }
  15. public DbSet<DayPlan> DayPlans { get; set; }
  16. // public DbSet<RecipeImage> RecipeImages { get; set; } // Temporarily disabled for main page performance
  17. protected override void OnModelCreating(ModelBuilder modelBuilder)
  18. {
  19. base.OnModelCreating(modelBuilder);
  20. modelBuilder.Entity<Recipe>(entity =>
  21. {
  22. // Map to existing lowercase table name
  23. entity.ToTable("recepie");
  24. entity.HasKey(e => e.Id);
  25. entity.Property(e => e.Id).HasColumnName("id");
  26. // Map our model properties to your database columns
  27. entity.Property(e => e.Title).HasColumnName("name").IsRequired();
  28. entity.Property(e => e.Description).HasColumnName("description");
  29. entity.Property(e => e.Difficulty).HasColumnName("difficulty");
  30. entity.Property(e => e.Url).HasColumnName("url");
  31. entity.Property(e => e.Time).HasColumnName("time");
  32. // Index for better search performance
  33. entity.HasIndex(e => e.Title);
  34. });
  35. modelBuilder.Entity<Ingredient>(entity =>
  36. {
  37. entity.ToTable("ingredient");
  38. entity.HasKey(e => e.Id);
  39. entity.Property(e => e.Id).HasColumnName("id");
  40. entity.Property(e => e.Name).HasColumnName("name");
  41. });
  42. modelBuilder.Entity<RecipeIngredientLink>(entity =>
  43. {
  44. entity.ToTable("recepieIngredientLink");
  45. entity.HasKey(e => new { e.RecipeId, e.IngredientId });
  46. entity.Property(e => e.RecipeId).HasColumnName("recepieId");
  47. entity.Property(e => e.IngredientId).HasColumnName("ingredientId");
  48. entity.Property(e => e.Amount).HasColumnName("amount");
  49. entity.Property(e => e.Measurement).HasColumnName("measurement");
  50. // Configure relationships
  51. entity.HasOne(e => e.Recipe)
  52. .WithMany(r => r.RecipeIngredients)
  53. .HasForeignKey(e => e.RecipeId);
  54. entity.HasOne(e => e.Ingredient)
  55. .WithMany()
  56. .HasForeignKey(e => e.IngredientId);
  57. });
  58. modelBuilder.Entity<RecipeStep>(entity =>
  59. {
  60. entity.ToTable("recepieSteps");
  61. entity.HasKey(e => e.Id);
  62. entity.Property(e => e.Id).HasColumnName("id");
  63. entity.Property(e => e.RecipeId).HasColumnName("recepieId");
  64. entity.Property(e => e.StepNumber).HasColumnName("stepNumber");
  65. entity.Property(e => e.Text).HasColumnName("text");
  66. // Configure relationship
  67. entity.HasOne(e => e.Recipe)
  68. .WithMany(r => r.RecipeSteps)
  69. .HasForeignKey(e => e.RecipeId);
  70. });
  71. // Week Planner Configuration
  72. modelBuilder.Entity<WeekPlan>(entity =>
  73. {
  74. entity.ToTable("weekplan");
  75. entity.HasKey(e => e.Id);
  76. entity.Property(e => e.Id).HasColumnName("id");
  77. entity.Property(e => e.Name).HasColumnName("name").HasMaxLength(255);
  78. entity.Property(e => e.StartDate).HasColumnName("startdate");
  79. entity.Property(e => e.CreatedAt).HasColumnName("createdat");
  80. });
  81. modelBuilder.Entity<DayPlan>(entity =>
  82. {
  83. entity.ToTable("dayplan");
  84. entity.HasKey(e => e.Id);
  85. entity.Property(e => e.Id).HasColumnName("id");
  86. entity.Property(e => e.WeekPlanId).HasColumnName("weekplanid");
  87. entity.Property(e => e.DayOfWeek).HasColumnName("dayofweek");
  88. entity.Property(e => e.Date).HasColumnName("date");
  89. entity.Property(e => e.RecipeId).HasColumnName("recipeid");
  90. entity.Property(e => e.MainIngredient).HasColumnName("mainingredient").HasMaxLength(100);
  91. entity.Property(e => e.BannedIngredients).HasColumnName("bannedingredients").HasMaxLength(500);
  92. entity.Property(e => e.RequiredIngredients).HasColumnName("requiredingredients").HasMaxLength(500);
  93. entity.HasOne(d => d.WeekPlan)
  94. .WithMany(p => p.DayPlans)
  95. .HasForeignKey(d => d.WeekPlanId);
  96. entity.HasOne(d => d.Recipe)
  97. .WithMany()
  98. .HasForeignKey(d => d.RecipeId);
  99. });
  100. // Temporarily disabled RecipeImage configuration for performance
  101. /*
  102. modelBuilder.Entity<RecipeImage>(entity =>
  103. {
  104. entity.ToTable("recepieImage");
  105. entity.HasKey(e => e.RecipeId);
  106. entity.Property(e => e.RecipeId).HasColumnName("recepieId");
  107. entity.Property(e => e.ImageData).HasColumnName("image");
  108. // Configure relationship
  109. entity.HasOne(e => e.Recipe)
  110. .WithOne(r => r.RecipeImage)
  111. .HasForeignKey<RecipeImage>(e => e.RecipeId);
  112. });
  113. */
  114. }
  115. }
  116. }