Files
webzine/Webzine.EntitiesContext/WebzineDbContext.cs

106 lines
3.4 KiB
C#

// <copyright file="WebzineDbContext.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace Webzine.EntitiesContext
{
using Microsoft.EntityFrameworkCore;
using Webzine.Entity;
public class WebzineDbContext : DbContext
{
/// <summary>
/// Initializes a new instance of the <see cref="WebzineDbContext"/> class.
/// </summary>
/// <param name="options">Options.</param>
public WebzineDbContext(DbContextOptions<WebzineDbContext> options)
: base(options) { }
/// <summary>
/// Gets Obtient les artistes de la base.
/// </summary>
public DbSet<Artiste> Artistes => this.Set<Artiste>();
/// <summary>
/// Gets Obtient les styles de la base.
/// </summary>
public DbSet<Style> Styles => this.Set<Style>();
/// <summary>
/// Gets obtient les titres de la base.
/// </summary>
public DbSet<Titre> Titres => this.Set<Titre>();
/// <summary>
/// Gets obtient les commentaires de la base.
/// </summary>
public DbSet<Commentaire> Commentaires => this.Set<Commentaire>();
/// <inheritdoc/>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Artiste>(entity =>
{
entity.ToTable("Artistes");
entity.HasKey(a => a.IdArtiste)
.HasName("PK_Artiste");
entity.HasMany(a => a.Titres)
.WithOne(t => t.Artiste)
.HasForeignKey(t => t.IdArtiste)
.OnDelete(DeleteBehavior.Restrict);
});
modelBuilder.Entity<Style>(entity =>
{
entity.ToTable("Styles");
entity.HasKey(s => s.IdStyle)
.HasName("PK_Style");
});
modelBuilder.Entity<Commentaire>(entity =>
{
entity.ToTable("Commentaires");
entity.HasKey(c => c.IdCommentaire)
.HasName("PK_Commentaire");
entity.HasOne(c => c.Titre)
.WithMany(t => t.Commentaires)
.HasForeignKey(c => c.IdTitre)
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity<Titre>(entity =>
{
entity.ToTable("Titres");
entity.HasKey(t => t.IdTitre)
.HasName("PK_Titre");
entity.HasMany(t => t.Styles)
.WithMany(s => s.Titres)
.UsingEntity<Dictionary<string, object>>(
"TitreStyle",
right => right.HasOne<Style>()
.WithMany()
.HasForeignKey("IdStyle")
.OnDelete(DeleteBehavior.Cascade),
left => left.HasOne<Titre>()
.WithMany()
.HasForeignKey("IdTitre")
.OnDelete(DeleteBehavior.Cascade),
join =>
{
join.HasKey("IdTitre", "IdStyle");
join.ToTable("TitreStyle");
});
});
}
}
}