using CoreDatabase.Models.Department; using CoreDatabase.Models.Security; using Microsoft.EntityFrameworkCore; namespace CoreDatabase { public class DatabaseContext : DbContext { public DatabaseContext() : base() { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (optionsBuilder.IsConfigured == false) { #if RELEASE var connectionString = ToolsModule.ManagmentDependency.ServiceProviderLoader.GetConfigData("connectionString"); optionsBuilder.UseSqlServer(connectionString); #endif #if DEBUG optionsBuilder.UseSqlServer(@"Data Source=CHESHIR\SQLEXPRESS;Initial Catalog=DepartmentDatabasePortal;persist security info=True;user id=admin;password=cheshirSA123;MultipleActiveResultSets=True;"); #endif } base.OnConfiguring(optionsBuilder); } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity().HasIndex(s => s.Key).IsUnique(); modelBuilder.Entity().HasIndex(s => s.RoleName).IsUnique(); modelBuilder.Entity().HasIndex(s => s.UserName); modelBuilder.Entity().HasIndex(s => s.PostName).IsUnique(); modelBuilder.Entity().HasIndex(d => new { d.FirstName, d.LastName, d.Patronymic }).IsUnique(); modelBuilder.Entity().HasIndex(d => new { d.Number }).IsUnique(); modelBuilder.Entity().HasIndex(d => new { d.Title }).IsUnique(); modelBuilder.Entity().HasIndex(d => new { d.DisciplineName }).IsUnique(); modelBuilder.Entity().HasIndex(d => new { d.LecturerAcademicDegreeName }).IsUnique(); modelBuilder.Entity().HasIndex(d => new { d.LecturerAcademicRankName }).IsUnique(); modelBuilder.Entity().HasIndex(d => new { d.FirstName, d.LastName, d.Patronymic }).IsUnique(); modelBuilder.Entity().HasIndex(d => new { d.Title, d.Profile }).IsUnique(); modelBuilder.Entity().HasIndex(d => new { d.TimeNormName, d.TimeNormShortName }).IsUnique(); modelBuilder.Entity().HasIndex(d => new { d.EducationDirectionId, d.YearStart }).IsUnique(); modelBuilder.Entity().HasIndex(d => new { d.AcademicPlanId, d.DisciplineId, d.Semester }).IsUnique(); modelBuilder.Entity().HasIndex(d => new { d.AcademicPlanRecordId, d.TimeNormId }).IsUnique(); // ругается на циклическое каскадное удаление, так что по нормам времени убираем ее modelBuilder.Entity() .HasOne(x => x.TimeNorm) .WithMany(x => x.AcademicPlanRecordTimeNormHours) .OnDelete(DeleteBehavior.NoAction); modelBuilder.Entity().HasIndex(d => new { d.AcademicPlanId, d.YearEntrance, d.GroupNumber }).IsUnique(); modelBuilder.Entity().HasIndex(d => new { d.NumberOfBook }).IsUnique(); modelBuilder.Entity().HasIndex(d => new { d.OrderNumber, d.OrderDate, d.OrderType }).IsUnique(); modelBuilder.Entity().HasIndex(d => new { d.StudentId, d.OrderId }).IsUnique(); modelBuilder.Entity() .HasOne(x => x.StudentGroupFrom) .WithMany(p => p.OrderStudentRecordFroms) .HasForeignKey(pt => pt.StudentGroupFromId) .OnDelete(DeleteBehavior.SetNull); modelBuilder.Entity() .HasOne(x => x.StudentGroupTo) .WithMany(p => p.OrderStudentRecordTos) .HasForeignKey(pt => pt.StudentGroupToId) .OnDelete(DeleteBehavior.NoAction); // ругается на циклическое каскадное удаление, так что по нормам времени убираем ее modelBuilder.Entity() .HasOne(x => x.Student) .WithMany(x => x.OrderStudentRecords) .OnDelete(DeleteBehavior.NoAction); modelBuilder.Entity().HasIndex(d => new { d.StudentId, d.DisciplineId, d.Semester, d.Mark }).IsUnique(); } #region Security public virtual DbSet Accesses { set; get; } public virtual DbSet EnviromentSettings { set; get; } public virtual DbSet Roles { set; get; } public virtual DbSet Users { set; get; } public virtual DbSet UserRoles { set; get; } #endregion #region Department public virtual DbSet Posts { set; get; } public virtual DbSet Employees { set; get; } public virtual DbSet EmployeePosts { set; get; } public virtual DbSet Classrooms { set; get; } public virtual DbSet DisciplineBlocks { set; get; } public virtual DbSet Disciplines { set; get; } public virtual DbSet LecturerAcademicDegrees { set; get; } public virtual DbSet LecturerAcademicRanks { set; get; } public virtual DbSet LecturerPosts { set; get; } public virtual DbSet Lecturers { set; get; } public virtual DbSet EducationDirections { set; get; } public virtual DbSet TimeNorms { set; get; } public virtual DbSet AcademicPlans { set; get; } public virtual DbSet AcademicPlanRecords { set; get; } public virtual DbSet AcademicPlanRecordTimeNormHours { set; get; } public virtual DbSet StudentGroups { set; get; } public virtual DbSet Students { set; get; } public virtual DbSet Orders { set; get; } public virtual DbSet OrderStudentRecords { set; get; } public virtual DbSet OrderSyncHistories { set; get; } public virtual DbSet OrderSyncHistoryRecords { set; get; } public virtual DbSet StudentMarkPassedDisciplines { set; get; } public virtual DbSet StudentMarkSyncHistories { set; get; } public virtual DbSet StudentMarkSyncHistoryRecords { set; get; } #endregion } }