48 lines
1.3 KiB
C#
48 lines
1.3 KiB
C#
using System;
|
|
using System.Linq;
|
|
using HN.Application;
|
|
using HN.Domain;
|
|
using HN.Infrastructure.Identity;
|
|
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace HN.Infrastructure
|
|
{
|
|
public sealed class HNDbContext : IdentityDbContext<User, Role, Guid>, IHNContext
|
|
{
|
|
private readonly ILoggerFactory _loggerFactory;
|
|
public DbSet<Link> Links { get; set; }
|
|
public DbSet<Comment> Comments { get; set; }
|
|
|
|
IQueryable<IUser> IHNContext.Users => Users;
|
|
|
|
public HNDbContext()
|
|
{
|
|
|
|
}
|
|
|
|
public HNDbContext(DbContextOptions<HNDbContext> options, ILoggerFactory loggerFactory) : base(options)
|
|
{
|
|
_loggerFactory = loggerFactory;
|
|
}
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
base.OnModelCreating(modelBuilder);
|
|
modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
|
|
}
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
// Si pas déjà configuré plus tôt dans la chaîne, comme c'est le cas dans l'appli Asp.Net Core
|
|
if (!optionsBuilder.IsConfigured)
|
|
{
|
|
optionsBuilder.UseSqlite("Data Source=:memory:");
|
|
}
|
|
|
|
optionsBuilder.UseLoggerFactory(_loggerFactory);
|
|
}
|
|
}
|
|
}
|