Selector/Selector.Model/ApplicationDbContext.cs

55 lines
1.6 KiB
C#
Raw Normal View History

2021-10-23 22:16:37 +01:00
using System;
using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
2021-10-24 00:23:45 +01:00
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
2021-10-23 22:16:37 +01:00
namespace Selector.Model
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
2021-10-23 22:16:37 +01:00
{
public DbSet<Watcher> Watcher { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
2021-10-23 22:16:37 +01:00
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
2021-10-23 22:16:37 +01:00
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
2021-10-24 00:23:45 +01:00
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Watcher>()
.HasOne(w => w.User)
.WithMany(u => u.Watchers)
.HasForeignKey(w => w.UserId);
SeedData.Seed(modelBuilder);
2021-10-23 22:16:37 +01:00
}
}
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
2021-10-23 22:16:37 +01:00
{
public ApplicationDbContext CreateDbContext(string[] args)
2021-10-23 22:16:37 +01:00
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(@Directory.GetCurrentDirectory() + "/../Selector.Web/appsettings.Development.json")
.Build();
var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
2021-10-23 22:16:37 +01:00
builder.UseNpgsql(configuration.GetConnectionString("Default"));
return new ApplicationDbContext(builder.Options);
2021-10-23 22:16:37 +01:00
}
}
}