relation test (collection)

main
syneffort 2 years ago
parent 1c98b870d8
commit d632b3abba
  1. 16
      BlazorApp/SharedData/Models/Book.cs
  2. 17
      BlazorApp/SharedData/Models/Review.cs
  3. 2
      BlazorApp/WebAPI/Data/ApplicationDbContext.cs
  4. 111
      BlazorApp/WebAPI/Migrations/20230828042927_book-review.Designer.cs
  5. 62
      BlazorApp/WebAPI/Migrations/20230828042927_book-review.cs
  6. 113
      BlazorApp/WebAPI/Migrations/20230828043008_book-review2.Designer.cs
  7. 59
      BlazorApp/WebAPI/Migrations/20230828043008_book-review2.cs
  8. 57
      BlazorApp/WebAPI/Migrations/ApplicationDbContextModelSnapshot.cs

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SharedData.Models
{
public class Book
{
public int BookId { get; set; }
public string Title { get; set; }
public string ISBN { get; set; }
public ICollection<Review> Reviews { get; set; }
}
}

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SharedData.Models
{
public class Review
{
public int ReviewId { get; set; }
//[ForeignKey("Book")]
public int BookId { get; set; }
public string Context { get; set; }
}
}

@ -6,6 +6,8 @@ namespace WebAPI.Data
public class ApplicationDbContext : DbContext public class ApplicationDbContext : DbContext
{ {
public DbSet<GameResult> GameResults { get; set; } public DbSet<GameResult> GameResults { get; set; }
public DbSet<Book> Books { get; set; }
public DbSet<Review> Reviews { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options) : base(options)

@ -0,0 +1,111 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using WebAPI.Data;
#nullable disable
namespace WebAPI.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20230828042927_book-review")]
partial class bookreview
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.10")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("SharedData.Models.Book", b =>
{
b.Property<int>("BookId")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("BookId"));
b.Property<string>("ISBN")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Title")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("BookId");
b.ToTable("Books");
});
modelBuilder.Entity("SharedData.Models.GameResult", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<DateTime>("Date")
.HasColumnType("datetime2");
b.Property<int>("Score")
.HasColumnType("int");
b.Property<int>("UserId")
.HasColumnType("int");
b.Property<string>("UserName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("GameResults");
});
modelBuilder.Entity("SharedData.Models.Review", b =>
{
b.Property<int>("ReviewId")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("ReviewId"));
b.Property<int?>("BookId")
.HasColumnType("int");
b.Property<string>("Context")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("ReviewId");
b.HasIndex("BookId");
b.ToTable("Reviews");
});
modelBuilder.Entity("SharedData.Models.Review", b =>
{
b.HasOne("SharedData.Models.Book", null)
.WithMany("Reviews")
.HasForeignKey("BookId");
});
modelBuilder.Entity("SharedData.Models.Book", b =>
{
b.Navigation("Reviews");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,62 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace WebAPI.Migrations
{
/// <inheritdoc />
public partial class bookreview : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Books",
columns: table => new
{
BookId = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Title = table.Column<string>(type: "nvarchar(max)", nullable: false),
ISBN = table.Column<string>(type: "nvarchar(max)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Books", x => x.BookId);
});
migrationBuilder.CreateTable(
name: "Reviews",
columns: table => new
{
ReviewId = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Context = table.Column<string>(type: "nvarchar(max)", nullable: false),
BookId = table.Column<int>(type: "int", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Reviews", x => x.ReviewId);
table.ForeignKey(
name: "FK_Reviews_Books_BookId",
column: x => x.BookId,
principalTable: "Books",
principalColumn: "BookId");
});
migrationBuilder.CreateIndex(
name: "IX_Reviews_BookId",
table: "Reviews",
column: "BookId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Reviews");
migrationBuilder.DropTable(
name: "Books");
}
}
}

@ -0,0 +1,113 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using WebAPI.Data;
#nullable disable
namespace WebAPI.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20230828043008_book-review2")]
partial class bookreview2
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.10")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("SharedData.Models.Book", b =>
{
b.Property<int>("BookId")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("BookId"));
b.Property<string>("ISBN")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Title")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("BookId");
b.ToTable("Books");
});
modelBuilder.Entity("SharedData.Models.GameResult", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<DateTime>("Date")
.HasColumnType("datetime2");
b.Property<int>("Score")
.HasColumnType("int");
b.Property<int>("UserId")
.HasColumnType("int");
b.Property<string>("UserName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("GameResults");
});
modelBuilder.Entity("SharedData.Models.Review", b =>
{
b.Property<int>("ReviewId")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("ReviewId"));
b.Property<int>("BookId")
.HasColumnType("int");
b.Property<string>("Context")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("ReviewId");
b.HasIndex("BookId");
b.ToTable("Reviews");
});
modelBuilder.Entity("SharedData.Models.Review", b =>
{
b.HasOne("SharedData.Models.Book", null)
.WithMany("Reviews")
.HasForeignKey("BookId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("SharedData.Models.Book", b =>
{
b.Navigation("Reviews");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,59 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace WebAPI.Migrations
{
/// <inheritdoc />
public partial class bookreview2 : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Reviews_Books_BookId",
table: "Reviews");
migrationBuilder.AlterColumn<int>(
name: "BookId",
table: "Reviews",
type: "int",
nullable: false,
defaultValue: 0,
oldClrType: typeof(int),
oldType: "int",
oldNullable: true);
migrationBuilder.AddForeignKey(
name: "FK_Reviews_Books_BookId",
table: "Reviews",
column: "BookId",
principalTable: "Books",
principalColumn: "BookId",
onDelete: ReferentialAction.Cascade);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Reviews_Books_BookId",
table: "Reviews");
migrationBuilder.AlterColumn<int>(
name: "BookId",
table: "Reviews",
type: "int",
nullable: true,
oldClrType: typeof(int),
oldType: "int");
migrationBuilder.AddForeignKey(
name: "FK_Reviews_Books_BookId",
table: "Reviews",
column: "BookId",
principalTable: "Books",
principalColumn: "BookId");
}
}
}

@ -22,6 +22,27 @@ namespace WebAPI.Migrations
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("SharedData.Models.Book", b =>
{
b.Property<int>("BookId")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("BookId"));
b.Property<string>("ISBN")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Title")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("BookId");
b.ToTable("Books");
});
modelBuilder.Entity("SharedData.Models.GameResult", b => modelBuilder.Entity("SharedData.Models.GameResult", b =>
{ {
b.Property<int>("Id") b.Property<int>("Id")
@ -47,6 +68,42 @@ namespace WebAPI.Migrations
b.ToTable("GameResults"); b.ToTable("GameResults");
}); });
modelBuilder.Entity("SharedData.Models.Review", b =>
{
b.Property<int>("ReviewId")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("ReviewId"));
b.Property<int>("BookId")
.HasColumnType("int");
b.Property<string>("Context")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("ReviewId");
b.HasIndex("BookId");
b.ToTable("Reviews");
});
modelBuilder.Entity("SharedData.Models.Review", b =>
{
b.HasOne("SharedData.Models.Book", null)
.WithMany("Reviews")
.HasForeignKey("BookId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("SharedData.Models.Book", b =>
{
b.Navigation("Reviews");
});
#pragma warning restore 612, 618 #pragma warning restore 612, 618
} }
} }

Loading…
Cancel
Save