diff --git a/WebAPIWithEF/Migrations/20240611080745_ModelRevisions.Designer.cs b/WebAPIWithEF/Migrations/20240611080745_ModelRevisions.Designer.cs new file mode 100644 index 0000000..816e137 --- /dev/null +++ b/WebAPIWithEF/Migrations/20240611080745_ModelRevisions.Designer.cs @@ -0,0 +1,123 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using WebAPIWithEF.Data; + +#nullable disable + +namespace WebAPIWithEF.Migrations +{ + [DbContext(typeof(PizzaContext))] + [Migration("20240611080745_ModelRevisions")] + partial class ModelRevisions + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "8.0.6"); + + modelBuilder.Entity("PizzaTopping", b => + { + b.Property("PizzasId") + .HasColumnType("INTEGER"); + + b.Property("ToppingsId") + .HasColumnType("INTEGER"); + + b.HasKey("PizzasId", "ToppingsId"); + + b.HasIndex("ToppingsId"); + + b.ToTable("PizzaTopping"); + }); + + modelBuilder.Entity("WebAPIWithEF.Models.Pizza", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("TEXT"); + + b.Property("SauceId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("SauceId"); + + b.ToTable("Pizzas"); + }); + + modelBuilder.Entity("WebAPIWithEF.Models.Sauce", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("IsVegan") + .HasColumnType("INTEGER"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Sauces"); + }); + + modelBuilder.Entity("WebAPIWithEF.Models.Topping", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Calories") + .HasColumnType("TEXT"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Toppings"); + }); + + modelBuilder.Entity("PizzaTopping", b => + { + b.HasOne("WebAPIWithEF.Models.Pizza", null) + .WithMany() + .HasForeignKey("PizzasId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("WebAPIWithEF.Models.Topping", null) + .WithMany() + .HasForeignKey("ToppingsId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("WebAPIWithEF.Models.Pizza", b => + { + b.HasOne("WebAPIWithEF.Models.Sauce", "Sauce") + .WithMany() + .HasForeignKey("SauceId"); + + b.Navigation("Sauce"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/WebAPIWithEF/Migrations/20240611080745_ModelRevisions.cs b/WebAPIWithEF/Migrations/20240611080745_ModelRevisions.cs new file mode 100644 index 0000000..bbc1b10 --- /dev/null +++ b/WebAPIWithEF/Migrations/20240611080745_ModelRevisions.cs @@ -0,0 +1,162 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace WebAPIWithEF.Migrations +{ + /// + public partial class ModelRevisions : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Toppings_Pizzas_PizzaId", + table: "Toppings"); + + migrationBuilder.DropIndex( + name: "IX_Toppings_PizzaId", + table: "Toppings"); + + migrationBuilder.DropColumn( + name: "PizzaId", + table: "Toppings"); + + migrationBuilder.AlterColumn( + name: "Name", + table: "Toppings", + type: "TEXT", + maxLength: 100, + nullable: false, + defaultValue: "", + oldClrType: typeof(string), + oldType: "TEXT", + oldNullable: true); + + migrationBuilder.AddColumn( + name: "Calories", + table: "Toppings", + type: "TEXT", + nullable: false, + defaultValue: 0m); + + migrationBuilder.AlterColumn( + name: "Name", + table: "Sauces", + type: "TEXT", + maxLength: 100, + nullable: false, + defaultValue: "", + oldClrType: typeof(string), + oldType: "TEXT", + oldNullable: true); + + migrationBuilder.AddColumn( + name: "IsVegan", + table: "Sauces", + type: "INTEGER", + nullable: false, + defaultValue: false); + + migrationBuilder.AlterColumn( + name: "Name", + table: "Pizzas", + type: "TEXT", + maxLength: 100, + nullable: false, + defaultValue: "", + oldClrType: typeof(string), + oldType: "TEXT", + oldNullable: true); + + migrationBuilder.CreateTable( + name: "PizzaTopping", + columns: table => new + { + PizzasId = table.Column(type: "INTEGER", nullable: false), + ToppingsId = table.Column(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_PizzaTopping", x => new { x.PizzasId, x.ToppingsId }); + table.ForeignKey( + name: "FK_PizzaTopping_Pizzas_PizzasId", + column: x => x.PizzasId, + principalTable: "Pizzas", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_PizzaTopping_Toppings_ToppingsId", + column: x => x.ToppingsId, + principalTable: "Toppings", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_PizzaTopping_ToppingsId", + table: "PizzaTopping", + column: "ToppingsId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "PizzaTopping"); + + migrationBuilder.DropColumn( + name: "Calories", + table: "Toppings"); + + migrationBuilder.DropColumn( + name: "IsVegan", + table: "Sauces"); + + migrationBuilder.AlterColumn( + name: "Name", + table: "Toppings", + type: "TEXT", + nullable: true, + oldClrType: typeof(string), + oldType: "TEXT", + oldMaxLength: 100); + + migrationBuilder.AddColumn( + name: "PizzaId", + table: "Toppings", + type: "INTEGER", + nullable: true); + + migrationBuilder.AlterColumn( + name: "Name", + table: "Sauces", + type: "TEXT", + nullable: true, + oldClrType: typeof(string), + oldType: "TEXT", + oldMaxLength: 100); + + migrationBuilder.AlterColumn( + name: "Name", + table: "Pizzas", + type: "TEXT", + nullable: true, + oldClrType: typeof(string), + oldType: "TEXT", + oldMaxLength: 100); + + migrationBuilder.CreateIndex( + name: "IX_Toppings_PizzaId", + table: "Toppings", + column: "PizzaId"); + + migrationBuilder.AddForeignKey( + name: "FK_Toppings_Pizzas_PizzaId", + table: "Toppings", + column: "PizzaId", + principalTable: "Pizzas", + principalColumn: "Id"); + } + } +} diff --git a/WebAPIWithEF/Migrations/PizzaContextModelSnapshot.cs b/WebAPIWithEF/Migrations/PizzaContextModelSnapshot.cs index abc4b7a..759300a 100644 --- a/WebAPIWithEF/Migrations/PizzaContextModelSnapshot.cs +++ b/WebAPIWithEF/Migrations/PizzaContextModelSnapshot.cs @@ -17,6 +17,21 @@ namespace WebAPIWithEF.Migrations #pragma warning disable 612, 618 modelBuilder.HasAnnotation("ProductVersion", "8.0.6"); + modelBuilder.Entity("PizzaTopping", b => + { + b.Property("PizzasId") + .HasColumnType("INTEGER"); + + b.Property("ToppingsId") + .HasColumnType("INTEGER"); + + b.HasKey("PizzasId", "ToppingsId"); + + b.HasIndex("ToppingsId"); + + b.ToTable("PizzaTopping"); + }); + modelBuilder.Entity("WebAPIWithEF.Models.Pizza", b => { b.Property("Id") @@ -24,6 +39,8 @@ namespace WebAPIWithEF.Migrations .HasColumnType("INTEGER"); b.Property("Name") + .IsRequired() + .HasMaxLength(100) .HasColumnType("TEXT"); b.Property("SauceId") @@ -42,7 +59,12 @@ namespace WebAPIWithEF.Migrations .ValueGeneratedOnAdd() .HasColumnType("INTEGER"); + b.Property("IsVegan") + .HasColumnType("INTEGER"); + b.Property("Name") + .IsRequired() + .HasMaxLength(100) .HasColumnType("TEXT"); b.HasKey("Id"); @@ -56,38 +78,41 @@ namespace WebAPIWithEF.Migrations .ValueGeneratedOnAdd() .HasColumnType("INTEGER"); - b.Property("Name") + b.Property("Calories") .HasColumnType("TEXT"); - b.Property("PizzaId") - .HasColumnType("INTEGER"); + b.Property("Name") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("TEXT"); b.HasKey("Id"); - b.HasIndex("PizzaId"); - b.ToTable("Toppings"); }); - modelBuilder.Entity("WebAPIWithEF.Models.Pizza", b => + modelBuilder.Entity("PizzaTopping", b => { - b.HasOne("WebAPIWithEF.Models.Sauce", "Sauce") + b.HasOne("WebAPIWithEF.Models.Pizza", null) .WithMany() - .HasForeignKey("SauceId"); + .HasForeignKey("PizzasId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); - b.Navigation("Sauce"); - }); - - modelBuilder.Entity("WebAPIWithEF.Models.Topping", b => - { - b.HasOne("WebAPIWithEF.Models.Pizza", null) - .WithMany("Toppings") - .HasForeignKey("PizzaId"); + b.HasOne("WebAPIWithEF.Models.Topping", null) + .WithMany() + .HasForeignKey("ToppingsId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); }); modelBuilder.Entity("WebAPIWithEF.Models.Pizza", b => { - b.Navigation("Toppings"); + b.HasOne("WebAPIWithEF.Models.Sauce", "Sauce") + .WithMany() + .HasForeignKey("SauceId"); + + b.Navigation("Sauce"); }); #pragma warning restore 612, 618 } diff --git a/WebAPIWithEF/Models/Pizza.cs b/WebAPIWithEF/Models/Pizza.cs index 7215897..6bf7df7 100644 --- a/WebAPIWithEF/Models/Pizza.cs +++ b/WebAPIWithEF/Models/Pizza.cs @@ -1,10 +1,17 @@ -namespace WebAPIWithEF.Models +using System.ComponentModel.DataAnnotations; + +namespace WebAPIWithEF.Models { public class Pizza { public int Id { get; set; } + + [Required] + [MaxLength(100)] public string? Name { get; set; } + public Sauce? Sauce { get; set; } + public ICollection? Toppings { get; set; } } } diff --git a/WebAPIWithEF/Models/Sauce.cs b/WebAPIWithEF/Models/Sauce.cs index b3cf2d1..b7db182 100644 --- a/WebAPIWithEF/Models/Sauce.cs +++ b/WebAPIWithEF/Models/Sauce.cs @@ -1,8 +1,15 @@ -namespace WebAPIWithEF.Models +using System.ComponentModel.DataAnnotations; + +namespace WebAPIWithEF.Models { public class Sauce { public int Id { get; set; } + + [Required] + [MaxLength(100)] public string? Name { get; set; } + + public bool IsVegan { get; set; } } } diff --git a/WebAPIWithEF/Models/Topping.cs b/WebAPIWithEF/Models/Topping.cs index e4c9747..c1641c0 100644 --- a/WebAPIWithEF/Models/Topping.cs +++ b/WebAPIWithEF/Models/Topping.cs @@ -1,8 +1,19 @@ -namespace WebAPIWithEF.Models +using System.ComponentModel.DataAnnotations; +using System.Text.Json.Serialization; + +namespace WebAPIWithEF.Models { public class Topping { public int Id { get; set; } + + [Required] + [MaxLength(100)] public string? Name { get; set; } + + public decimal Calories { get; set; } + + [JsonIgnore] + public ICollection? Pizzas { get; set; } } }