DepartmentProject/DepartmentPortal/Common/CoreDatabase/DatabaseContext.cs

130 lines
5.8 KiB
C#
Raw Normal View History

2022-03-18 22:55:48 +04:00
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");
2022-03-18 22:55:48 +04:00
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<EnviromentSetting>().HasIndex(s => s.Key).IsUnique();
modelBuilder.Entity<Role>().HasIndex(s => s.RoleName).IsUnique();
modelBuilder.Entity<User>().HasIndex(s => s.UserName);
modelBuilder.Entity<Post>().HasIndex(s => s.PostName).IsUnique();
modelBuilder.Entity<Employee>().HasIndex(d => new { d.FirstName, d.LastName, d.Patronymic }).IsUnique();
modelBuilder.Entity<Classroom>().HasIndex(d => new { d.Number }).IsUnique();
modelBuilder.Entity<DisciplineBlock>().HasIndex(d => new { d.Title }).IsUnique();
modelBuilder.Entity<Discipline>().HasIndex(d => new { d.DisciplineName }).IsUnique();
modelBuilder.Entity<LecturerAcademicDegree>().HasIndex(d => new { d.LecturerAcademicDegreeName }).IsUnique();
modelBuilder.Entity<LecturerAcademicRank>().HasIndex(d => new { d.LecturerAcademicRankName }).IsUnique();
modelBuilder.Entity<Lecturer>().HasIndex(d => new { d.FirstName, d.LastName, d.Patronymic }).IsUnique();
modelBuilder.Entity<EducationDirection>().HasIndex(d => new { d.Title, d.Profile }).IsUnique();
modelBuilder.Entity<TimeNorm>().HasIndex(d => new { d.TimeNormName, d.TimeNormShortName }).IsUnique();
modelBuilder.Entity<AcademicPlan>().HasIndex(d => new { d.EducationDirectionId, d.CreateDate }).IsUnique();
2022-03-18 22:55:48 +04:00
modelBuilder.Entity<AcademicPlanRecord>().HasIndex(d => new { d.AcademicPlanId, d.DisciplineId, d.Semester }).IsUnique();
modelBuilder.Entity<AcademicPlanRecordTimeNormHour>().HasIndex(d => new { d.AcademicPlanRecordId, d.TimeNormId }).IsUnique();
modelBuilder.Entity<EnrollmentYear>().HasIndex(d => new { d.AcademicPlanId, d.YearEntrance }).IsUnique();
2022-03-18 22:55:48 +04:00
// ругается на циклическое каскадное удаление, так что по нормам времени убираем ее
modelBuilder.Entity<AcademicPlanRecordTimeNormHour>()
.HasOne(x => x.TimeNorm)
.WithMany(x => x.AcademicPlanRecordTimeNormHours)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<StudentGroup>().HasIndex(d => new { d.EducationDirectionId, d.AcademicCourse, d.GroupNumber }).IsUnique();
modelBuilder.Entity<Student>().HasIndex(d => new { d.NumberOfBook }).IsUnique();
modelBuilder.Entity<Order>().HasIndex(d => new { d.OrderNumber }).IsUnique();
modelBuilder.Entity<OrderStudentRecord>().HasIndex(d => new { d.StudentId, d.OrderId }).IsUnique();
modelBuilder.Entity<OrderStudentRecord>()
.HasOne(x => x.StudentGroupFrom)
.WithMany(p => p.OrderStudentRecordFroms)
.HasForeignKey(pt => pt.StudentGroupFromId)
.OnDelete(DeleteBehavior.SetNull);
modelBuilder.Entity<OrderStudentRecord>()
.HasOne(x => x.StudentGroupTo)
.WithMany(p => p.OrderStudentRecordTos)
.HasForeignKey(pt => pt.StudentGroupToId)
.OnDelete(DeleteBehavior.NoAction);
// ругается на циклическое каскадное удаление, так что по нормам времени убираем ее
modelBuilder.Entity<OrderStudentRecord>()
.HasOne(x => x.Student)
.WithMany(x => x.OrderStudentRecords)
.OnDelete(DeleteBehavior.NoAction);
}
#region Security
public virtual DbSet<Access> Accesses { set; get; }
public virtual DbSet<EnviromentSetting> EnviromentSettings { set; get; }
public virtual DbSet<Role> Roles { set; get; }
public virtual DbSet<User> Users { set; get; }
public virtual DbSet<UserRole> UserRoles { set; get; }
#endregion
#region Department
public virtual DbSet<Post> Posts { set; get; }
public virtual DbSet<Employee> Employees { set; get; }
public virtual DbSet<EmployeePost> EmployeePosts { set; get; }
public virtual DbSet<Classroom> Classrooms { set; get; }
public virtual DbSet<DisciplineBlock> DisciplineBlocks { set; get; }
public virtual DbSet<Discipline> Disciplines { set; get; }
public virtual DbSet<LecturerAcademicDegree> LecturerAcademicDegrees { set; get; }
public virtual DbSet<LecturerAcademicRank> LecturerAcademicRanks { set; get; }
public virtual DbSet<LecturerPost> LecturerPosts { set; get; }
public virtual DbSet<Lecturer> Lecturers { set; get; }
public virtual DbSet<EducationDirection> EducationDirections { set; get; }
public virtual DbSet<TimeNorm> TimeNorms { set; get; }
public virtual DbSet<AcademicPlan> AcademicPlans { set; get; }
public virtual DbSet<AcademicPlanRecord> AcademicPlanRecords { set; get; }
public virtual DbSet<AcademicPlanRecordTimeNormHour> AcademicPlanRecordTimeNormHours { set; get; }
public virtual DbSet<StudentGroup> StudentGroups { set; get; }
public virtual DbSet<Student> Students { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<OrderStudentRecord> OrderStudentRecords { set; get; }
public virtual DbSet<OrderSyncHistory> OrderSyncHistories { set; get; }
public virtual DbSet<OrderSyncHistoryRecord> OrderSyncHistoryRecords { set; get; }
public virtual DbSet<EnrollmentYear> EnrollmentYears { set; get; }
2022-03-18 22:55:48 +04:00
#endregion
}
}