using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using OliverBooth.Common.Data.Blog.Configuration;
namespace OliverBooth.Common.Data.Blog;
///
/// Represents a session with the blog database.
///
internal 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 collection of blog posts in the database.
///
/// The collection of blog posts.
public DbSet BlogPosts { get; private set; } = null!;
///
/// Gets the collection of blog posts drafts in the database.
///
/// The collection of blog post drafts.
public DbSet BlogPostDrafts { get; private 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 BlogPostConfiguration());
modelBuilder.ApplyConfiguration(new BlogPostDraftConfiguration());
}
}