features/cutover-to-ef #9
39
Newsbot.Collector.Database/DatabaseContext.cs
Normal file
39
Newsbot.Collector.Database/DatabaseContext.cs
Normal file
@ -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<ArticlesEntity> Articles { get; set; } = null!;
|
||||
public DbSet<DiscordQueueEntity> DiscordQueue { get; set; } = null!;
|
||||
public DbSet<DiscordWebhookEntity> DiscordWebhooks { get; set; } = null!;
|
||||
public DbSet<IconEntity> Icons { get; set; } = null!;
|
||||
public DbSet<SourceEntity> Sources { get; set; } = null!;
|
||||
public DbSet<SubscriptionEntity> 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<DatabaseContext> options)
|
||||
: base(options)
|
||||
{
|
||||
}
|
||||
}
|
15
Newsbot.Collector.Database/DesignTimeContext.cs
Normal file
15
Newsbot.Collector.Database/DesignTimeContext.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
|
||||
namespace Newsbot.Collector.Database;
|
||||
|
||||
public class DesignTimeContext :IDesignTimeDbContextFactory<DatabaseContext>
|
||||
{
|
||||
public DatabaseContext CreateDbContext(string[] args)
|
||||
{
|
||||
var optionsBuilder = new DbContextOptionsBuilder<DatabaseContext>();
|
||||
// pass your design time connection string here
|
||||
optionsBuilder.UseNpgsql("<connection_string>");
|
||||
return new DatabaseContext(optionsBuilder.Options);
|
||||
}
|
||||
}
|
@ -6,8 +6,14 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="dapper" Version="2.0.123" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.7" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.7">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
|
||||
<PackageReference Include="Npgsql" Version="7.0.2" />
|
||||
<PackageReference Include="Npgsql" Version="7.0.4" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
|
Loading…
Reference in New Issue
Block a user