oliverbooth.dev/OliverBooth.Common/Data/Blog/BlogContext.cs

50 lines
1.7 KiB
C#
Raw Normal View History

2023-08-11 15:51:20 +01:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using OliverBooth.Common.Data.Blog.Configuration;
namespace OliverBooth.Common.Data.Blog;
/// <summary>
/// Represents a session with the blog database.
/// </summary>
internal sealed class BlogContext : DbContext
{
private readonly IConfiguration _configuration;
/// <summary>
/// Initializes a new instance of the <see cref="BlogContext" /> class.
/// </summary>
/// <param name="configuration">The configuration.</param>
public BlogContext(IConfiguration configuration)
{
_configuration = configuration;
}
/// <summary>
/// Gets the collection of blog posts in the database.
/// </summary>
/// <value>The collection of blog posts.</value>
public DbSet<BlogPost> BlogPosts { get; private set; } = null!;
/// <summary>
/// Gets the collection of blog posts drafts in the database.
/// </summary>
/// <value>The collection of blog post drafts.</value>
public DbSet<BlogPostDraft> BlogPostDrafts { get; private set; } = null!;
/// <inheritdoc />
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string connectionString = _configuration.GetConnectionString("Blog") ?? string.Empty;
ServerVersion serverVersion = ServerVersion.AutoDetect(connectionString);
optionsBuilder.UseMySql(connectionString, serverVersion);
}
/// <inheritdoc />
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new BlogPostConfiguration());
modelBuilder.ApplyConfiguration(new BlogPostDraftConfiguration());
}
}