hn-dotnet/Infrastructure/HNDbContext.cs
Julien Leicher 3cd5133f66 add-aspnet-identity (#26)
add exception filter when user not connected
default to needing authentication and apply anonymous to some actions
add user in get requests
add user relation in link, comment and vote
signup and in are ok now!
2020-12-11 17:59:35 +01:00

47 lines
1.3 KiB
C#

using System;
using System.Linq;
using HN.Application;
using HN.Domain;
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);
}
}
}