2023-08-11 15:51:20 +01:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-03-02 00:43:56 +00:00
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using OliverBooth.Common.Data.Blog.Configuration;
|
2023-08-06 15:57:23 +01:00
|
|
|
|
2024-03-02 00:43:56 +00:00
|
|
|
namespace OliverBooth.Common.Data.Blog;
|
2023-08-06 15:57:23 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Represents a session with the blog database.
|
|
|
|
/// </summary>
|
2023-08-12 20:13:47 +01:00
|
|
|
internal sealed class BlogContext : DbContext
|
2023-08-06 15:57:23 +01:00
|
|
|
{
|
|
|
|
private readonly IConfiguration _configuration;
|
|
|
|
|
2023-08-08 21:04:02 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="BlogContext" /> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="configuration">The configuration.</param>
|
2023-08-06 15:57:23 +01:00
|
|
|
public BlogContext(IConfiguration configuration)
|
|
|
|
{
|
|
|
|
_configuration = configuration;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2023-08-12 20:13:47 +01:00
|
|
|
/// Gets the collection of blog posts in the database.
|
2023-08-06 15:57:23 +01:00
|
|
|
/// </summary>
|
2023-08-12 20:13:47 +01:00
|
|
|
/// <value>The collection of blog posts.</value>
|
|
|
|
public DbSet<BlogPost> BlogPosts { get; private set; } = null!;
|
2023-08-06 15:57:23 +01:00
|
|
|
|
2024-02-29 18:06:30 +00:00
|
|
|
/// <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!;
|
|
|
|
|
2023-08-06 15:57:23 +01:00
|
|
|
/// <inheritdoc />
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
|
|
{
|
|
|
|
string connectionString = _configuration.GetConnectionString("Blog") ?? string.Empty;
|
|
|
|
ServerVersion serverVersion = ServerVersion.AutoDetect(connectionString);
|
|
|
|
optionsBuilder.UseMySql(connectionString, serverVersion);
|
|
|
|
}
|
|
|
|
|
2023-08-08 21:04:02 +01:00
|
|
|
/// <inheritdoc />
|
2023-08-06 15:57:23 +01:00
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
|
|
{
|
|
|
|
modelBuilder.ApplyConfiguration(new BlogPostConfiguration());
|
2024-02-29 18:06:30 +00:00
|
|
|
modelBuilder.ApplyConfiguration(new BlogPostDraftConfiguration());
|
2023-08-06 15:57:23 +01:00
|
|
|
}
|
|
|
|
}
|