PIbd-21_Pyatakov_KM_Markov_.../UniversityDataBaseImplemet/Database.cs
2023-05-20 05:46:02 +04:00

69 lines
2.8 KiB
C#

using Microsoft.EntityFrameworkCore;
using UniversityDataBaseImplemet.Models;
using Stream = UniversityDataBaseImplemet.Models.Stream;
namespace UniversityDataBaseImplemet
{
public class Database : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseNpgsql("Host=localhost;Port=5432;Database=UniversityCourseWork;Username=postgres;Password=0000");
}
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<StudentDocument>()
.HasKey(x => new { x.StudentId, x.DocumentId });
modelBuilder.Entity<EducationGroupDocument>()
.HasKey(x => new { x.EducationGroupId, x.DocumentId });
modelBuilder.Entity<EducationGroupStream>()
.HasKey(x => new { x.EducationGroupId, x.StreamId });
modelBuilder.Entity<StudentStream>()
.HasKey(x => new { x.StudentId, x.StreamId });
modelBuilder.Entity<Document>()
.HasOne(b => b.User)
.WithMany(a => a.Documents)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<EducationStatus>()
.HasOne(b => b.User)
.WithMany(a => a.EducationStatuses)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<Student>()
.HasOne(b => b.User)
.WithMany(a => a.Students)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<Discipline>()
.HasOne(b => b.User)
.WithMany(a => a.Disciplines)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<EducationGroup>()
.HasOne(b => b.User)
.WithMany(a => a.EducationGroups)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<Stream>()
.HasOne(b => b.User)
.WithMany(a => a.Streams)
.OnDelete(DeleteBehavior.NoAction);
}
public virtual DbSet<User> User { get; set; }
public virtual DbSet<Document> Documents { get; set; }
public virtual DbSet<Discipline> Discipline { get; set; }
public virtual DbSet<EducationStatus> EducationStatuses { get; set; }
public virtual DbSet<EducationGroup> EducationGroups { get; set; }
public virtual DbSet<EducationGroupDocument> EducationGroupsDocuments { get; set; }
public virtual DbSet<EducationGroupStream> EducationGroupsStreams { get; set; }
public virtual DbSet<Stream> Streams { get; set; }
public virtual DbSet<Student> Students { get; set; }
public virtual DbSet<StudentDocument> StudentDocuments { get; set; }
public virtual DbSet<StudentStream> StudentStreams { get; set; }
}
}