Ajout de la base de données SQLite
This commit is contained in:
@@ -8,6 +8,8 @@
|
||||
|
||||
<ItemGroup>
|
||||
<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="StyleCop.Analyzers" Version="1.1.118">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
@@ -21,4 +23,8 @@
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Webzine.Entity\Webzine.Entity.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
97
Webzine.EntitiesContext/WebzineDbContext.cs
Normal file
97
Webzine.EntitiesContext/WebzineDbContext.cs
Normal 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");
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,8 @@
|
||||
|
||||
<ItemGroup>
|
||||
<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="StyleCop.Analyzers" Version="1.1.118">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
|
||||
1
Webzine.WebApplication/Data/.gitignore
vendored
Normal file
1
Webzine.WebApplication/Data/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
*.sqlite*
|
||||
@@ -1,5 +1,7 @@
|
||||
using NLog;
|
||||
using NLog.Web;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Webzine.EntitiesContext;
|
||||
using Webzine.Repository;
|
||||
using Webzine.Repository.Contracts;
|
||||
|
||||
@@ -21,6 +23,9 @@ try
|
||||
|
||||
builder.Services.AddSingleton<ITitreRepository, LocalEntityRepository>();
|
||||
|
||||
builder.Services.AddDbContext<WebzineDbContext>(options =>
|
||||
options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection")));
|
||||
|
||||
// NLog: Setup NLog for Dependency injection
|
||||
builder.Logging.ClearProviders();
|
||||
builder.Host.UseNLog();
|
||||
@@ -31,6 +36,12 @@ try
|
||||
// le dossier wwwroot.
|
||||
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.
|
||||
app.UseRouting();
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Data\" />
|
||||
<Folder Include="wwwroot\data\" />
|
||||
<Folder Include="wwwroot\lib\" />
|
||||
</ItemGroup>
|
||||
@@ -24,6 +25,8 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Faker.Net" Version="2.0.163" />
|
||||
<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" Version="6.1.1" />
|
||||
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
|
||||
@@ -33,6 +36,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Webzine.EntitiesContext\Webzine.EntitiesContext.csproj" />
|
||||
<ProjectReference Include="..\Webzine.Entity\Webzine.Entity.csproj" />
|
||||
<ProjectReference Include="..\Webzine.Repository\Webzine.Repository.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -9,5 +9,8 @@
|
||||
"NombreDerniereChronique": 3,
|
||||
"NombreDeTopTitres": 3
|
||||
},
|
||||
"ConnectionStrings": {
|
||||
"DefaultConnection": "Data Source=Data/webzine.sqlite"
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user