From 8149ce21517974315083f6f498a4476d03dff7ed Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Mon, 19 Jun 2023 15:36:43 -0700 Subject: [PATCH] ef was added to the project --- Newsbot.Collector.Database/DatabaseContext.cs | 39 +++++++++++++++++++ .../DesignTimeContext.cs | 15 +++++++ .../Newsbot.Collector.Database.csproj | 8 +++- 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 Newsbot.Collector.Database/DatabaseContext.cs create mode 100644 Newsbot.Collector.Database/DesignTimeContext.cs diff --git a/Newsbot.Collector.Database/DatabaseContext.cs b/Newsbot.Collector.Database/DatabaseContext.cs new file mode 100644 index 0000000..46e1cf9 --- /dev/null +++ b/Newsbot.Collector.Database/DatabaseContext.cs @@ -0,0 +1,39 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Newsbot.Collector.Domain.Consts; +using Newsbot.Collector.Domain.Entities; + +namespace Newsbot.Collector.Database; + +public class DatabaseContext : DbContext +{ + public DbSet Articles { get; set; } = null!; + public DbSet DiscordQueue { get; set; } = null!; + public DbSet DiscordWebhooks { get; set; } = null!; + public DbSet Icons { get; set; } = null!; + public DbSet Sources { get; set; } = null!; + public DbSet Subscriptions { get; set; } = null!; + + private string ConnectionString { get; set; } + + public DatabaseContext(IConfiguration appsettings) + { + var connString = appsettings.GetConnectionString(ConfigConnectionStringConst.Database); + ConnectionString = connString ?? ""; + } + + public DatabaseContext(string connectionString) + { + ConnectionString = connectionString; + } + + protected override void OnConfiguring(DbContextOptionsBuilder options) + { + options.UseNpgsql(ConnectionString); + } + + public DatabaseContext(DbContextOptions options) + : base(options) + { + } +} \ No newline at end of file diff --git a/Newsbot.Collector.Database/DesignTimeContext.cs b/Newsbot.Collector.Database/DesignTimeContext.cs new file mode 100644 index 0000000..ab717c9 --- /dev/null +++ b/Newsbot.Collector.Database/DesignTimeContext.cs @@ -0,0 +1,15 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Design; + +namespace Newsbot.Collector.Database; + +public class DesignTimeContext :IDesignTimeDbContextFactory +{ + public DatabaseContext CreateDbContext(string[] args) + { + var optionsBuilder = new DbContextOptionsBuilder(); + // pass your design time connection string here + optionsBuilder.UseNpgsql(""); + return new DatabaseContext(optionsBuilder.Options); + } +} \ No newline at end of file diff --git a/Newsbot.Collector.Database/Newsbot.Collector.Database.csproj b/Newsbot.Collector.Database/Newsbot.Collector.Database.csproj index 8f50477..c191f7f 100644 --- a/Newsbot.Collector.Database/Newsbot.Collector.Database.csproj +++ b/Newsbot.Collector.Database/Newsbot.Collector.Database.csproj @@ -6,8 +6,14 @@ + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + - + +