RecipeContext.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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<RecipeImage> RecipeImages { get; set; } // Temporarily disabled for main page performance
  15. protected override void OnModelCreating(ModelBuilder modelBuilder)
  16. {
  17. base.OnModelCreating(modelBuilder);
  18. modelBuilder.Entity<Recipe>(entity =>
  19. {
  20. // Map to existing lowercase table name
  21. entity.ToTable("recepie");
  22. entity.HasKey(e => e.Id);
  23. entity.Property(e => e.Id).HasColumnName("id");
  24. // Map our model properties to your database columns
  25. entity.Property(e => e.Title).HasColumnName("name").IsRequired();
  26. entity.Property(e => e.Description).HasColumnName("description");
  27. entity.Property(e => e.Difficulty).HasColumnName("difficulty");
  28. entity.Property(e => e.Url).HasColumnName("url");
  29. entity.Property(e => e.Time).HasColumnName("time");
  30. // Index for better search performance
  31. entity.HasIndex(e => e.Title);
  32. });
  33. modelBuilder.Entity<Ingredient>(entity =>
  34. {
  35. entity.ToTable("ingredient");
  36. entity.HasKey(e => e.Id);
  37. entity.Property(e => e.Id).HasColumnName("id");
  38. entity.Property(e => e.Name).HasColumnName("name");
  39. });
  40. modelBuilder.Entity<RecipeIngredientLink>(entity =>
  41. {
  42. entity.ToTable("recepieIngredientLink");
  43. entity.HasKey(e => new { e.RecipeId, e.IngredientId });
  44. entity.Property(e => e.RecipeId).HasColumnName("recepieId");
  45. entity.Property(e => e.IngredientId).HasColumnName("ingredientId");
  46. entity.Property(e => e.Amount).HasColumnName("amount");
  47. entity.Property(e => e.Measurement).HasColumnName("measurement");
  48. // Configure relationships
  49. entity.HasOne(e => e.Recipe)
  50. .WithMany(r => r.RecipeIngredients)
  51. .HasForeignKey(e => e.RecipeId);
  52. entity.HasOne(e => e.Ingredient)
  53. .WithMany()
  54. .HasForeignKey(e => e.IngredientId);
  55. });
  56. modelBuilder.Entity<RecipeStep>(entity =>
  57. {
  58. entity.ToTable("recepieSteps");
  59. entity.HasKey(e => e.Id);
  60. entity.Property(e => e.Id).HasColumnName("id");
  61. entity.Property(e => e.RecipeId).HasColumnName("recepieId");
  62. entity.Property(e => e.StepNumber).HasColumnName("stepNumber");
  63. entity.Property(e => e.Text).HasColumnName("text");
  64. // Configure relationship
  65. entity.HasOne(e => e.Recipe)
  66. .WithMany(r => r.RecipeSteps)
  67. .HasForeignKey(e => e.RecipeId);
  68. });
  69. // Temporarily disabled RecipeImage configuration for performance
  70. /*
  71. modelBuilder.Entity<RecipeImage>(entity =>
  72. {
  73. entity.ToTable("recepieImage");
  74. entity.HasKey(e => e.RecipeId);
  75. entity.Property(e => e.RecipeId).HasColumnName("recepieId");
  76. entity.Property(e => e.ImageData).HasColumnName("image");
  77. // Configure relationship
  78. entity.HasOne(e => e.Recipe)
  79. .WithOne(r => r.RecipeImage)
  80. .HasForeignKey<RecipeImage>(e => e.RecipeId);
  81. });
  82. */
  83. }
  84. }
  85. }