Ajout de la base de données SQLite

This commit is contained in:
Loic Masi
2026-03-25 11:23:14 +01:00
parent cdd130ae21
commit cc87f1850d
7 changed files with 125 additions and 2 deletions

View File

@@ -8,6 +8,8 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Faker.Net" Version="2.0.163" /> <PackageReference Include="Faker.Net" Version="2.0.163" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.5" />
<PackageReference Include="NLog" Version="6.1.1" /> <PackageReference Include="NLog" Version="6.1.1" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118"> <PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
@@ -21,4 +23,8 @@
</Content> </Content>
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Webzine.Entity\Webzine.Entity.csproj" />
</ItemGroup>
</Project> </Project>

View File

@@ -0,0 +1,97 @@
// <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.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.HasKey(s => s.IdStyle)
.HasName("PK_Style");
});
modelBuilder.Entity<Commentaire>(entity =>
{
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.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");
});
});
}
}
}

View File

@@ -8,7 +8,8 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Faker.Net" Version="2.0.163" /> <PackageReference Include="Faker.Net" Version="2.0.163" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.3" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.5" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.5" />
<PackageReference Include="NLog" Version="6.1.1" /> <PackageReference Include="NLog" Version="6.1.1" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118"> <PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>

View File

@@ -0,0 +1 @@
*.sqlite*

View File

@@ -1,5 +1,7 @@
using NLog; using NLog;
using NLog.Web; using NLog.Web;
using Microsoft.EntityFrameworkCore;
using Webzine.EntitiesContext;
using Webzine.Repository; using Webzine.Repository;
using Webzine.Repository.Contracts; using Webzine.Repository.Contracts;
@@ -21,6 +23,9 @@ try
builder.Services.AddSingleton<ITitreRepository, LocalEntityRepository>(); builder.Services.AddSingleton<ITitreRepository, LocalEntityRepository>();
builder.Services.AddDbContext<WebzineDbContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection")));
// NLog: Setup NLog for Dependency injection // NLog: Setup NLog for Dependency injection
builder.Logging.ClearProviders(); builder.Logging.ClearProviders();
builder.Host.UseNLog(); builder.Host.UseNLog();
@@ -31,6 +36,12 @@ try
// le dossier wwwroot. // le dossier wwwroot.
app.UseStaticFiles(); app.UseStaticFiles();
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<WebzineDbContext>();
db.Database.EnsureCreated();
}
// Active le middleware permettant le routage des requetes entrantes. // Active le middleware permettant le routage des requetes entrantes.
app.UseRouting(); app.UseRouting();

View File

@@ -17,6 +17,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Data\" />
<Folder Include="wwwroot\data\" /> <Folder Include="wwwroot\data\" />
<Folder Include="wwwroot\lib\" /> <Folder Include="wwwroot\lib\" />
</ItemGroup> </ItemGroup>
@@ -24,6 +25,8 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Faker.Net" Version="2.0.163" /> <PackageReference Include="Faker.Net" Version="2.0.163" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="10.0.3" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="10.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.5" />
<PackageReference Include="NLog.Web.AspNetCore" Version="5.*" /> <PackageReference Include="NLog.Web.AspNetCore" Version="5.*" />
<PackageReference Include="NLog" Version="6.1.1" /> <PackageReference Include="NLog" Version="6.1.1" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118"> <PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
@@ -33,6 +36,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Webzine.EntitiesContext\Webzine.EntitiesContext.csproj" />
<ProjectReference Include="..\Webzine.Entity\Webzine.Entity.csproj" /> <ProjectReference Include="..\Webzine.Entity\Webzine.Entity.csproj" />
<ProjectReference Include="..\Webzine.Repository\Webzine.Repository.csproj" /> <ProjectReference Include="..\Webzine.Repository\Webzine.Repository.csproj" />
</ItemGroup> </ItemGroup>

View File

@@ -9,5 +9,8 @@
"NombreDerniereChronique": 3, "NombreDerniereChronique": 3,
"NombreDeTopTitres": 3 "NombreDeTopTitres": 3
}, },
"ConnectionStrings": {
"DefaultConnection": "Data Source=Data/webzine.sqlite"
},
"AllowedHosts": "*" "AllowedHosts": "*"
} }