using Microsoft.EntityFrameworkCore;
using OliverBooth.Data.Blog;
using OliverBooth.Data.Blog.Configuration;
namespace OliverBooth.Data;
///
/// Represents a session with the blog database.
///
public sealed class BlogContext : DbContext
{
private readonly IConfiguration _configuration;
///
/// Initializes a new instance of the class.
///
/// The configuration.
public BlogContext(IConfiguration configuration)
{
_configuration = configuration;
}
///
/// Gets the set of authors.
///
/// The set of authors.
public DbSet Authors { get; internal set; } = null!;
///
/// Gets the set of blog posts.
///
/// The set of blog posts.
public DbSet BlogPosts { get; internal set; } = null!;
///
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string connectionString = _configuration.GetConnectionString("Blog") ?? string.Empty;
ServerVersion serverVersion = ServerVersion.AutoDetect(connectionString);
optionsBuilder.UseMySql(connectionString, serverVersion);
}
///
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new AuthorConfiguration());
modelBuilder.ApplyConfiguration(new BlogPostConfiguration());
}
}