-
Notifications
You must be signed in to change notification settings - Fork 0
/
RelationContext.cs
60 lines (52 loc) · 2.33 KB
/
RelationContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using Microsoft.EntityFrameworkCore;
namespace ConsoleApp1
{
class RelationContext : DbContext
{
public RelationContext() : base()
{
// Database.SetInitializer<SchoolDBContext>(new CreateDatabaseIfNotExists<SchoolDBContext>());
}
public DbSet<Address> Adresses { get; set; }
public DbSet<Student> Students { get; set; }
//public DbSet<Course> Courses { get; set; }
public DbSet<Section> Sections { get; set; }
public DbSet<Course> Courses { get; set; }
public DbSet<StuCorse> SCTable {get;set;}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=.;Database=StudentDatabase;Trusted_Connection=True;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<StuCorse>().HasKey(cs => new { cs.StudentId, cs.CourseId});
//modelBuilder.Entity<User>().Property(user => user.Role).HasDefaultValue("Player");
}
/* protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>()
.HasMany<Course>(s => s.Courses)
.WithMany(c => c.Students)
.Map(cs =>
{
cs.MapLeftKey("StudentRefId");
cs.MapRightKey("CourseRefId");
cs.ToTable("StudentCourse");
});
}*/
/*protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Configure Student & StudentAddress entity
modelBuilder.Entity<Student>()
.HasOptional(s => s.Address) // Mark Address property optional in Student entity
.WithRequired(ad => ad.Student); // mark Student property as required in StudentAddress entity. Cannot save StudentAddress without Student
}*/
/*protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>().ToTable("Address");
modelBuilder.Entity<Course>().ToTable("CousreStudents");
}*/
}
}