using System; using System.Linq; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using RdtClient.Data.Models.Data; namespace RdtClient.Data.Data { public class DataContext : IdentityDbContext { public DataContext(DbContextOptions options) : base(options) { } public DataContext() { } public static String ConnectionString => $"Data Source={AppContext.BaseDirectory}database.db"; public DbSet Downloads { get; set; } public DbSet Settings { get; set; } public DbSet Torrents { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseSqlite(ConnectionString); protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); builder.Entity() .HasData(new Setting { SettingId = "RealDebridApiKey", Type = "String", Value = "" }); builder.Entity() .HasData(new Setting { SettingId = "DownloadFolder", Type = "String", Value = @"C:\Downloads" }); builder.Entity() .HasData(new Setting { SettingId = "DownloadLimit", Type = "Int32", Value = "10" }); var cascadeFKs = builder.Model.GetEntityTypes() .SelectMany(t => t.GetForeignKeys()) .Where(fk => !fk.IsOwnership && fk.DeleteBehavior == DeleteBehavior.Cascade); foreach (var fk in cascadeFKs) { fk.DeleteBehavior = DeleteBehavior.Restrict; } } public void Migrate() { Database.Migrate(); } } }