using Microsoft.EntityFrameworkCore;
using OliverBooth.Data.Web.Configuration;
namespace OliverBooth.Data.Web;
///
/// Represents a session with the web database.
///
internal sealed class WebContext : DbContext
{
private readonly IConfiguration _configuration;
///
/// Initializes a new instance of the class.
///
/// The configuration.
public WebContext(IConfiguration configuration)
{
_configuration = configuration;
}
///
/// Gets the collection of books in the reading list.
///
/// The collection of books.
public DbSet Books { get; private set; } = null!;
///
/// Gets the collection of blacklist entries in the database.
///
/// The collection of blacklist entries.
public DbSet ContactBlacklist { get; private set; } = null!;
///
/// Gets the collection of programming languages in the database.
///
/// The collection of programming languages.
public DbSet ProgrammingLanguages { get; private set; } = null!;
///
/// Gets the collection of projects in the database.
///
/// The collection of projects.
public DbSet Projects { get; private set; } = null!;
///
/// Gets the set of site configuration items.
///
/// The set of site configuration items.
public DbSet SiteConfiguration { get; private set; } = null!;
///
/// Gets the collection of templates in the database.
///
/// The collection of templates.
public DbSet Templates { get; private set; } = null!;
///
/// Gets the collection of users in the database.
///
/// The collection of users.
public DbSet Users { get; private set; } = null!;
///
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string connectionString = _configuration.GetConnectionString("Web") ?? string.Empty;
ServerVersion serverVersion = ServerVersion.AutoDetect(connectionString);
optionsBuilder.UseMySql(connectionString, serverVersion);
}
///
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new BlacklistEntryConfiguration());
modelBuilder.ApplyConfiguration(new BookConfiguration());
modelBuilder.ApplyConfiguration(new ProgrammingLanguageConfiguration());
modelBuilder.ApplyConfiguration(new ProjectConfiguration());
modelBuilder.ApplyConfiguration(new TemplateConfiguration());
modelBuilder.ApplyConfiguration(new SiteConfigurationConfiguration());
modelBuilder.ApplyConfiguration(new UserConfiguration());
}
}