rdt-client/server/RdtClient.Data/Data/DataContext.cs
2020-04-07 12:08:17 -06:00

70 lines
No EOL
2.1 KiB
C#

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<Download> Downloads { get; set; }
public DbSet<Setting> Settings { get; set; }
public DbSet<Torrent> Torrents { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseSqlite(ConnectionString);
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Setting>()
.HasData(new Setting
{
SettingId = "RealDebridApiKey",
Type = "String",
Value = ""
});
builder.Entity<Setting>()
.HasData(new Setting
{
SettingId = "DownloadFolder",
Type = "String",
Value = @"C:\Downloads"
});
builder.Entity<Setting>()
.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();
}
}
}