using Kruzya.TelegramBot.Core.EF;
using Microsoft.EntityFrameworkCore;
namespace Kruzya.TelegramBot.RichSiteSummary.Data
{
public class RichSiteSummaryContext : AutoSaveDbContext
{
///
/// Repository with all feeds in database.
///
public DbSet Feeds { get; set; }
///
/// Repository with all posts in database.
///
public DbSet Posts { get; set; }
///
/// Repository with all subscriptions in database.
///
public DbSet Subscriptions { get; set; }
///
/// Ensures the database is created and calls parent constructor.
///
///
public RichSiteSummaryContext(DbContextOptions options) : base(options)
{
Database.EnsureCreated();
}
///
/// Setup the additional unique keys.
///
///
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Setup unique keys for Feed entity.
modelBuilder.Entity().HasIndex(feed => feed.Url).IsUnique();
modelBuilder.Entity().HasIndex(feed => feed.HomePage).IsUnique();
// Setup unique keys for Post entity.
modelBuilder.Entity()
.HasOne(p => p.Feed)
.WithMany(f => f.Posts)
.HasForeignKey(p => p.FeedId);
modelBuilder.Entity().HasIndex(post =>
new {post.FeedId, post.Url}).IsUnique();
// Setup foreign keys for Subscriber entity.
modelBuilder.Entity()
.HasOne(s => s.Feed)
.WithMany(f => f.Subscribers)
.HasForeignKey(s => s.FeedId);
}
}
}