Migration

This commit is contained in:
2025-10-17 08:50:51 +02:00
parent d9accc9234
commit 8b04752d0e
4 changed files with 453 additions and 0 deletions

View File

@@ -26,5 +26,18 @@ public class BlogPlatformDbContext : DbContext
// Personnalisation du modèle (non utilisée ici)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Comment>()
.HasOne(c => c.User) // Un commentaire appartient à un seul utilisateur
.WithMany(u => u.Comments) // Un utilisateur peut avoir plusieurs commentaires
.HasForeignKey(c => c.UserId)
.OnDelete(DeleteBehavior.Restrict); // Evite la suppression en de l'utilisateur si des commentaires à lui le sont
modelBuilder.Entity<Comment>()
.HasOne(c => c.Post) // Un commentaire est lié à un seul post
.WithMany(p => p.Comments) // Un post peut avoir plusieurs commentaires
.HasForeignKey(c => c.PostId)
.OnDelete(DeleteBehavior.Cascade); // Si un post est supprimé alors les commentaires aussi
}
}

View File

@@ -0,0 +1,166 @@
// <auto-generated />
using System;
using BlogPlatform;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace BlogPlatform.Migrations
{
[DbContext(typeof(BlogPlatformDbContext))]
[Migration("20251017065027_InitDatabase")]
partial class InitDatabase
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.20")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("BlogPlatform.Models.Comment", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Content")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateOnly>("CreatedAt")
.HasColumnType("date");
b.Property<int>("PostId")
.HasColumnType("int");
b.Property<int>("UserId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("PostId");
b.HasIndex("UserId");
b.ToTable("Comments");
});
modelBuilder.Entity("BlogPlatform.Models.Post", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Content")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateOnly>("CreatedAt")
.HasColumnType("date");
b.Property<int>("Likes")
.HasColumnType("int");
b.Property<string>("Title")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("UserId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("Posts");
});
modelBuilder.Entity("BlogPlatform.Models.User", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<DateOnly>("CreatedAt")
.HasColumnType("date");
b.Property<string>("Email")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Salt")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Username")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Users");
});
modelBuilder.Entity("BlogPlatform.Models.Comment", b =>
{
b.HasOne("BlogPlatform.Models.Post", "Post")
.WithMany("Comments")
.HasForeignKey("PostId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("BlogPlatform.Models.User", "User")
.WithMany("Comments")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
b.Navigation("Post");
b.Navigation("User");
});
modelBuilder.Entity("BlogPlatform.Models.Post", b =>
{
b.HasOne("BlogPlatform.Models.User", "User")
.WithMany("Posts")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("User");
});
modelBuilder.Entity("BlogPlatform.Models.Post", b =>
{
b.Navigation("Comments");
});
modelBuilder.Entity("BlogPlatform.Models.User", b =>
{
b.Navigation("Comments");
b.Navigation("Posts");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,111 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace BlogPlatform.Migrations
{
/// <inheritdoc />
public partial class InitDatabase : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Username = table.Column<string>(type: "nvarchar(max)", nullable: false),
Email = table.Column<string>(type: "nvarchar(max)", nullable: false),
Password = table.Column<string>(type: "nvarchar(max)", nullable: false),
Salt = table.Column<string>(type: "nvarchar(max)", nullable: false),
CreatedAt = table.Column<DateOnly>(type: "date", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Posts",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Title = table.Column<string>(type: "nvarchar(max)", nullable: false),
Content = table.Column<string>(type: "nvarchar(max)", nullable: false),
Likes = table.Column<int>(type: "int", nullable: false),
CreatedAt = table.Column<DateOnly>(type: "date", nullable: false),
UserId = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Posts", x => x.Id);
table.ForeignKey(
name: "FK_Posts_Users_UserId",
column: x => x.UserId,
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "Comments",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Content = table.Column<string>(type: "nvarchar(max)", nullable: false),
CreatedAt = table.Column<DateOnly>(type: "date", nullable: false),
PostId = table.Column<int>(type: "int", nullable: false),
UserId = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Comments", x => x.Id);
table.ForeignKey(
name: "FK_Comments_Posts_PostId",
column: x => x.PostId,
principalTable: "Posts",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_Comments_Users_UserId",
column: x => x.UserId,
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_Comments_PostId",
table: "Comments",
column: "PostId");
migrationBuilder.CreateIndex(
name: "IX_Comments_UserId",
table: "Comments",
column: "UserId");
migrationBuilder.CreateIndex(
name: "IX_Posts_UserId",
table: "Posts",
column: "UserId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Comments");
migrationBuilder.DropTable(
name: "Posts");
migrationBuilder.DropTable(
name: "Users");
}
}
}

View File

@@ -0,0 +1,163 @@
// <auto-generated />
using System;
using BlogPlatform;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace BlogPlatform.Migrations
{
[DbContext(typeof(BlogPlatformDbContext))]
partial class BlogPlatformDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.20")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("BlogPlatform.Models.Comment", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Content")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateOnly>("CreatedAt")
.HasColumnType("date");
b.Property<int>("PostId")
.HasColumnType("int");
b.Property<int>("UserId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("PostId");
b.HasIndex("UserId");
b.ToTable("Comments");
});
modelBuilder.Entity("BlogPlatform.Models.Post", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Content")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateOnly>("CreatedAt")
.HasColumnType("date");
b.Property<int>("Likes")
.HasColumnType("int");
b.Property<string>("Title")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("UserId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("Posts");
});
modelBuilder.Entity("BlogPlatform.Models.User", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<DateOnly>("CreatedAt")
.HasColumnType("date");
b.Property<string>("Email")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Salt")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Username")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Users");
});
modelBuilder.Entity("BlogPlatform.Models.Comment", b =>
{
b.HasOne("BlogPlatform.Models.Post", "Post")
.WithMany("Comments")
.HasForeignKey("PostId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("BlogPlatform.Models.User", "User")
.WithMany("Comments")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
b.Navigation("Post");
b.Navigation("User");
});
modelBuilder.Entity("BlogPlatform.Models.Post", b =>
{
b.HasOne("BlogPlatform.Models.User", "User")
.WithMany("Posts")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("User");
});
modelBuilder.Entity("BlogPlatform.Models.Post", b =>
{
b.Navigation("Comments");
});
modelBuilder.Entity("BlogPlatform.Models.User", b =>
{
b.Navigation("Comments");
b.Navigation("Posts");
});
#pragma warning restore 612, 618
}
}
}