You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
4.1 KiB
102 lines
4.1 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Entity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
namespace Contextlib
|
|
{
|
|
public class WTFContext : DbContext
|
|
{
|
|
//public DbSet<Admin> admins { get; set; }
|
|
public DbSet<Character> characters { get; set; }
|
|
public DbSet<Commentary> comments { get; set; }
|
|
public DbSet<Favorite> favorites { get; set; }
|
|
public DbSet<Images> images { get; set; }
|
|
public DbSet<Question> question { get; set; }
|
|
public DbSet<Quiz> quizzes { get; set; }
|
|
//public DbSet<QuizQuestion> quizQuestions { get; set; }
|
|
public DbSet<Quote> quotes { get; set; }
|
|
//public DbSet<RecordQuiz> records { get; set; }
|
|
public DbSet<Source> sources { get; set; }
|
|
public DbSet<Users> users { get; set; }
|
|
public DbSet<Admin> admins { get; set; }
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
modelBuilder.Entity<Users>()
|
|
.HasMany(q => q.Favorite)
|
|
.WithMany(u => u.Favorite)
|
|
.UsingEntity<Favorite>(
|
|
l => l.HasOne(f => f.Quote)
|
|
.WithMany()
|
|
.OnDelete(DeleteBehavior.ClientCascade)
|
|
.HasForeignKey(f => f.IdQuote),
|
|
r => r.HasOne(f => f.Users)
|
|
.WithMany()
|
|
.OnDelete(DeleteBehavior.ClientCascade)
|
|
.HasForeignKey(f => f.IdUsers)
|
|
);
|
|
|
|
modelBuilder.Entity<Users>()
|
|
.HasMany(u => u.Quotes)
|
|
.WithOne(q => q.User)
|
|
.OnDelete(DeleteBehavior.ClientSetNull)
|
|
.HasForeignKey(q => q.IdUsersPropose);
|
|
|
|
modelBuilder.Entity<Users>()
|
|
.HasMany<Quote>()
|
|
.WithMany()
|
|
.UsingEntity<Commentary>(
|
|
i => i.HasKey(e => e.Id)
|
|
);
|
|
|
|
modelBuilder.Entity<Commentary>()
|
|
.HasOne(c => c.User)
|
|
.WithMany()
|
|
.HasForeignKey(c => c.IdUser);
|
|
|
|
modelBuilder.Entity<Quote>()
|
|
.HasMany(q => q.Commentarys)
|
|
.WithOne(c => c.Quote)
|
|
.OnDelete(DeleteBehavior.ClientCascade)
|
|
.HasForeignKey(c => c.IdQuote);
|
|
|
|
modelBuilder.Entity<Quiz>()
|
|
.HasMany(q => q.Questions)
|
|
.WithMany(u => u.Quizs)
|
|
.UsingEntity<QuizQuestion>(
|
|
l => l.HasOne<Question>().WithMany().HasForeignKey(q => q.IdQuestion),
|
|
r => r.HasOne<Quiz>().WithMany().HasForeignKey(u => u.IdQuiz)
|
|
);
|
|
//modelBuilder.Entity<Admin>()
|
|
// .HasOne(a => a.User)
|
|
// .WithOne(u => u.admin);
|
|
}
|
|
|
|
public WTFContext()
|
|
{ }
|
|
|
|
public WTFContext(DbContextOptions<WTFContext> options)
|
|
: base(options)
|
|
{ }
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder options)
|
|
{
|
|
if (!options.IsConfigured)
|
|
{
|
|
options.UseSqlServer(
|
|
$"Server={Environment.GetEnvironmentVariable("DB_SERVER_AUTH")};" +
|
|
$"Database={Environment.GetEnvironmentVariable("DB_DATABASE_AUTH")};" +
|
|
$"User Id={Environment.GetEnvironmentVariable("DB_USER_AUTH")};" +
|
|
$"Password={Environment.GetEnvironmentVariable("DB_PASSWORD_AUTH")};");
|
|
//options.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Wf-Database.mdf;Trusted_Connection=True;");
|
|
}
|
|
}
|
|
}
|
|
}
|