using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

namespace Agency
{
    public partial class gr606_midvlContext : DbContext
    {
        public gr606_midvlContext()
        {
        }

        public gr606_midvlContext(DbContextOptions<gr606_midvlContext> options)
            : base(options)
        {
        }

        public virtual DbSet<Addrese> Addreses { get; set; } = null!;
        public virtual DbSet<ListPerson> ListPeople { get; set; } = null!;
        public virtual DbSet<Passport> Passports { get; set; } = null!;
        public virtual DbSet<Person> Persons { get; set; } = null!;
        public virtual DbSet<TheProperty> TheProperties { get; set; } = null!;
        public virtual DbSet<Treaty> Treaties { get; set; } = null!;

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                //#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
                optionsBuilder.UseSqlServer("Data source=mssql;Database=gr606_midvl;Integrated Security=true;");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Addrese>(entity =>
            {
                entity.Property(e => e.Streat).HasMaxLength(100);
                entity.Property(e => e.City).HasMaxLength(100);
            });

            modelBuilder.Entity<ListPerson>(entity =>
            {
                entity.HasKey(e => e.Idlist);

                entity.ToTable("ListPerson");

                entity.Property(e => e.Idlist).HasColumnName("IDList");

                entity.Property(e => e.Idperson).HasColumnName("IDPerson");

                entity.Property(e => e.Idproperty).HasColumnName("IDProperty");

                entity.HasOne(d => d.IdpersonNavigation)
                    .WithMany(p => p.ListPeople)
                    .HasForeignKey(d => d.Idperson)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_ListPerson_Persons");

                entity.HasOne(d => d.IdpropertyNavigation)
                    .WithMany(p => p.ListPeople)
                    .HasForeignKey(d => d.Idproperty)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_ListPerson_TheProperty");
            });

            modelBuilder.Entity<Passport>(entity =>
            {
                entity.Property(e => e.DateOfIssue).HasColumnType("date");

                entity.Property(e => e.Issuedby).IsUnicode(false);

                entity.Property(e => e.Number).IsUnicode(false);

                entity.Property(e => e.Serial).IsUnicode(false);

                entity.HasOne(d => d.Owner)
                    .WithMany(p => p.Passports)
                    .HasForeignKey(d => d.OwnerId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_Passports_Persons");
            });

            modelBuilder.Entity<Person>(entity =>
            {
                entity.HasKey(e => e.IdPerson);

                entity.Property(e => e.IdPerson).ValueGeneratedOnAdd();

                entity.Property(e => e.FirstName).HasMaxLength(100);

                entity.Property(e => e.LastName).HasMaxLength(100);

                entity.Property(e => e.Login)
                    .HasMaxLength(100)
                    .IsUnicode(false);

                entity.Property(e => e.MidleName).HasMaxLength(100);

                entity.Property(e => e.Password)
                    .HasMaxLength(100)
                    .IsUnicode(false);

                entity.Property(e => e.PhoneNumber)
                    .HasMaxLength(100)
                    .IsUnicode(false);
            });

            modelBuilder.Entity<TheProperty>(entity =>
            {
                entity.ToTable("TheProperty");

                entity.Property(e => e.District)
                    .HasMaxLength(100)
                    .IsUnicode(false);

                entity.Property(e => e.Name)
                    .HasMaxLength(100)
                    .IsUnicode(false);

                entity.Property(e => e.Price).HasColumnType("decimal(18, 2)");

                entity.Property(e => e.PropertyType)
                    .HasMaxLength(100)
                    .IsUnicode(false);

                entity.Property(e => e.YearOfConstruction).HasColumnType("date");

                entity.HasOne(d => d.AddressNavigation)
                    .WithMany(p => p.TheProperties)
                    .HasForeignKey(d => d.Address)
                    .HasConstraintName("FK_TheProperty_Addreses");
                entity.Property(e => e.TypeRent)
                   .HasMaxLength(100)
                   .IsUnicode(false);
            });

            modelBuilder.Entity<Treaty>(entity =>
            {
                entity.HasKey(e => e.IdTreaty);

                entity.ToTable("Treaty");

                entity.Property(e => e.Cost).HasColumnType("decimal(18, 2)");

                entity.Property(e => e.DateOfConclusion).HasColumnType("date");

                entity.HasOne(d => d.PersonsInTreatyNavigation)
                    .WithMany(p => p.Treaties)
                    .HasForeignKey(d => d.PersonsInTreaty)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_Treaty_ListPerson");

                entity.HasOne(d => d.Property)
                    .WithMany(p => p.Treaties)
                    .HasForeignKey(d => d.PropertyId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_Treaty_TheProperty");
            });

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
}
