using Microsoft.EntityFrameworkCore;

namespace Work29
{
    public class Work29Context : DbContext
    {
        public Work29Context()
        {
            Database.EnsureCreated();
        }
        public DbSet<User> Users { get; set; }
        public DbSet<Task> Tasks { get; set; }
        public DbSet<StatusTask> StatusTasks { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseLazyLoadingProxies();
            optionsBuilder.UseSqlServer("Data Source=(localdb)\\mssqllocaldb; Initial Catalog=AppDb;Trusted_Connection=True;");
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Task>()
                        .HasOne(m => m.UserCreated)
                        .WithMany(t => t.UserCreateds)
                        .HasForeignKey(m => m.UserCreatedId)
                        .OnDelete(DeleteBehavior.Restrict);
            modelBuilder.Entity<Task>()
                        .HasOne(m => m.UserAccepted)
                        .WithMany(t => t.UserAccepteds)
                        .HasForeignKey(m => m.UserAcceptedId)
                        .OnDelete(DeleteBehavior.Restrict);
            modelBuilder.Entity<Task>()
                        .HasOne(m => m.StatusTask)
                        .WithMany(t => t.Tasks)
                        .HasForeignKey(m => m.StatusTaskId)
                        .OnDelete(DeleteBehavior.Restrict);

        }
    }
}
