Created
June 9, 2020 21:29
-
-
Save TheFo2sh/4f1bdac9a82249da25f4ec060fd4d4d0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.EntityFrameworkCore; | |
using TimeTable.Core.Models; | |
namespace TimeTableServer.Context | |
{ | |
public class DataContext : DbContext | |
{ | |
public DbSet<StudentCourse> StudentCourse { get; set; } | |
public DbSet<Student> Students { get; set; } | |
public DbSet<Teacher> Teachers { get; set; } | |
public DbSet<Course> Courses { get; set; } | |
public DbSet<Place> Places { get; set; } | |
public DbSet<TimeSlot> TimeSlots { get; set; } | |
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | |
{ | |
optionsBuilder.UseSqlServer( | |
@"Server=(localdb)\ProjectsV13;Database=TimeTableDb;Integrated Security=True"); | |
} | |
protected override void OnModelCreating(ModelBuilder modelBuilder) | |
{ | |
modelBuilder.Entity<StudentCourse>().HasKey("StudentId", "CourseId"); | |
modelBuilder.Entity<Student>().HasMany(student => student.Courses); | |
modelBuilder.Entity<Course>().HasMany(course => course.Students); | |
modelBuilder.Entity<Student>().HasMany<TimeSlot>(student => student.TimeSlots); | |
modelBuilder.Entity<TimeSlot>().HasMany<Student>(slot => slot.Students); | |
base.OnModelCreating(modelBuilder); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment