2023-08-11 15:51:20 +01:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-08-13 17:33:54 +01:00
|
|
|
using OliverBooth.Data.Blog.Configuration;
|
2024-02-24 15:27:03 +00:00
|
|
|
using OliverBooth.Data.Web;
|
2023-08-06 15:57:23 +01:00
|
|
|
|
2023-08-13 17:33:54 +01:00
|
|
|
namespace OliverBooth.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
|
|
|
|
|
|
|
/// <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());
|
|
|
|
}
|
|
|
|
}
|