Files

57 lines
1.8 KiB
C#
Raw Permalink Normal View History

2022-01-22 21:54:11 +03:00
using Kruzya.TelegramBot.Core.EF;
using Microsoft.EntityFrameworkCore;
2020-03-02 00:00:52 +04:00
2023-07-29 15:14:52 +03:00
namespace Kruzya.TelegramBot.RichSiteSummary.Data;
public class RichSiteSummaryContext : AutoSaveDbContext
2020-03-02 00:00:52 +04:00
{
2023-07-29 15:14:52 +03:00
/// <summary>
/// Repository with all feeds in database.
/// </summary>
public DbSet<Feed> Feeds { get; set; }
2020-03-02 00:00:52 +04:00
2023-07-29 15:14:52 +03:00
/// <summary>
/// Repository with all posts in database.
/// </summary>
public DbSet<Post> Posts { get; set; }
2020-03-02 00:00:52 +04:00
2023-07-29 15:14:52 +03:00
/// <summary>
/// Repository with all subscriptions in database.
/// </summary>
public DbSet<Subscriber> Subscriptions { get; set; }
2020-03-02 00:00:52 +04:00
2023-07-29 15:14:52 +03:00
/// <summary>
/// Ensures the database is created and calls parent constructor.
/// </summary>
/// <param name="options"></param>
public RichSiteSummaryContext(DbContextOptions<RichSiteSummaryContext> options) : base(options)
{
Database.EnsureCreated();
}
2020-03-02 00:00:52 +04:00
2023-07-29 15:14:52 +03:00
/// <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();
2020-03-02 00:00:52 +04:00
2023-07-29 15:14:52 +03:00
// Setup unique keys for Post entity.
modelBuilder.Entity<Post>()
.HasOne(p => p.Feed)
.WithMany(f => f.Posts)
.HasForeignKey(p => p.FeedId);
2020-03-02 00:00:52 +04:00
2023-07-29 15:14:52 +03:00
modelBuilder.Entity<Post>().HasIndex(post =>
new {post.FeedId, post.Url}).IsUnique();
2020-03-02 00:00:52 +04:00
2023-07-29 15:14:52 +03:00
// Setup foreign keys for Subscriber entity.
modelBuilder.Entity<Subscriber>()
.HasOne(s => s.Feed)
.WithMany(f => f.Subscribers)
.HasForeignKey(s => s.FeedId);
2020-03-02 00:00:52 +04:00
}
}