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 admins { get; set; } public DbSet characters { get; set; } public DbSet comments { get; set; } public DbSet favorites { get; set; } public DbSet images { get; set; } public DbSet question { get; set; } public DbSet quizzes { get; set; } //public DbSet quizQuestions { get; set; } public DbSet quotes { get; set; } //public DbSet records { get; set; } public DbSet sources { get; set; } public DbSet users { get; set; } public DbSet admins { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity() .HasMany(q => q.Favorite) .WithMany(u => u.Favorite) .UsingEntity( 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() .HasMany(u => u.Quotes) .WithOne(q => q.User) .OnDelete(DeleteBehavior.ClientSetNull) .HasForeignKey(q => q.IdUsersPropose); modelBuilder.Entity() .HasMany() .WithMany() .UsingEntity( i => i.HasKey(e => e.Id) ); modelBuilder.Entity() .HasOne(c => c.User) .WithMany() .HasForeignKey(c => c.IdUser); modelBuilder.Entity() .HasMany(q => q.Commentarys) .WithOne(c => c.Quote) .OnDelete(DeleteBehavior.ClientCascade) .HasForeignKey(c => c.IdQuote); modelBuilder.Entity() .HasMany(q => q.Questions) .WithMany(u => u.Quizs) .UsingEntity( l => l.HasOne().WithMany().HasForeignKey(q => q.IdQuestion), r => r.HasOne().WithMany().HasForeignKey(u => u.IdQuiz) ); //modelBuilder.Entity() // .HasOne(a => a.User) // .WithOne(u => u.admin); } public WTFContext() { } public WTFContext(DbContextOptions 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;"); } } } }