mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
57 lines
1.8 KiB
C#
57 lines
1.8 KiB
C#
using Kruzya.TelegramBot.Core.EF;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Kruzya.TelegramBot.RichSiteSummary.Data;
|
|
|
|
public class RichSiteSummaryContext : AutoSaveDbContext
|
|
{
|
|
/// <summary>
|
|
/// Repository with all feeds in database.
|
|
/// </summary>
|
|
public DbSet<Feed> Feeds { get; set; }
|
|
|
|
/// <summary>
|
|
/// Repository with all posts in database.
|
|
/// </summary>
|
|
public DbSet<Post> Posts { get; set; }
|
|
|
|
/// <summary>
|
|
/// Repository with all subscriptions in database.
|
|
/// </summary>
|
|
public DbSet<Subscriber> Subscriptions { get; set; }
|
|
|
|
/// <summary>
|
|
/// Ensures the database is created and calls parent constructor.
|
|
/// </summary>
|
|
/// <param name="options"></param>
|
|
public RichSiteSummaryContext(DbContextOptions<RichSiteSummaryContext> options) : base(options)
|
|
{
|
|
Database.EnsureCreated();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Setup the additional unique keys.
|
|
/// </summary>
|
|
/// <param name="modelBuilder"></param>
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
// Setup unique keys for Feed entity.
|
|
modelBuilder.Entity<Feed>().HasIndex(feed => feed.Url).IsUnique();
|
|
modelBuilder.Entity<Feed>().HasIndex(feed => feed.HomePage).IsUnique();
|
|
|
|
// Setup unique keys for Post entity.
|
|
modelBuilder.Entity<Post>()
|
|
.HasOne(p => p.Feed)
|
|
.WithMany(f => f.Posts)
|
|
.HasForeignKey(p => p.FeedId);
|
|
|
|
modelBuilder.Entity<Post>().HasIndex(post =>
|
|
new {post.FeedId, post.Url}).IsUnique();
|
|
|
|
// Setup foreign keys for Subscriber entity.
|
|
modelBuilder.Entity<Subscriber>()
|
|
.HasOne(s => s.Feed)
|
|
.WithMany(f => f.Subscribers)
|
|
.HasForeignKey(s => s.FeedId);
|
|
}
|
|
} |